我可以在C++中部分编译模板函数吗?
Can I partial compile a template function in C++
我有一个函数来确定模板类型是否为指针。
template<class T>
struct is_pointer_struct { static const bool value = false; };
template<class T>
struct is_pointer_struct<T*> { static const bool value = true; };
template<class T>
bool is_pointer(T &var) {
return is_pointer_struct<T>::value;
}
我有一个初始化函数。
template<class T>
void initialize(T &val) {
if (is_pointer(val))
val = NULL;
else
val = T();
}
显然,当T
string
时,这段代码是无法编译的。有没有办法在T
是指针类型时编译val = NULL
,当T
不是指针类型时编译val = T()
?
在您的特定情况下,您可以使用统一初始化,如 VTT 所说:
val = T{};
此外,标准库还提供std::is_pointer
.
作为对更一般的问题"如何在编译时分支?">的回答:
在 C++17 中,您所要做的就是将
if(...)
更改为if constexpr(...)
:template<class T> void initialize(T &val) { if constexpr(is_pointer(val)) val = nullptr; else val = T(); }
在 C++14 中,您可以实现自己的
static_if
。在 C++03/11 中,您可以使用标记调度:
template <typename T> void initialize_impl(std::true_type /* pointer */, T& val) { val = NULL; } template <typename T> void initialize_impl(std::false_type /* non-pointer */, T& val) { val = T(); } template<class T> void initialize(T &val) { initialize_impl(std::is_pointer<T>{}, val); }
如前所述,在您的情况下执行操作的正确方法是使用统一初始化。
作为一种选择,您可以根据您的类型特征使用SFINAE,以便实例化必要的模板(这是C++ 11方法(:
template<class T>
auto initialize(T &val) ->
typename std::enable_if<is_pointer_struct<T>::value>::type {
val = nullptr;
}
template<class T>
auto initialize(T &val) ->
typename std::enable_if<!is_pointer_struct<T>::value>::type {
val = T();
}
经典解决方案,甚至不需要 C++11 功能: 简单重载:
template<class T>
void initialize(T& val)
{
val = T();
}
template<class T>
void initialize(T*& val)
{
val = NULL;
}
但是,第一个重载(在特定情况下(也涵盖了指针,因此第二个重载实际上已经过时了。
不考虑过时,我更喜欢nullptr
关键字而不是NULL
宏(尽管再次失去了 C++11 之前的兼容性(。
相关文章:
- 跨模板化函数编译的静态变量
- 无法使用 LoadObject() 函数编译 UE4 Actor。
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 函数编译,即使它不接受整数
- 使用C 模板函数编译时间递归
- 安卓 JNI - 'raw'函数编译失败
- C++就地析构函数编译警告
- 对 TR1 使用 boost 时提升数学特殊函数编译错误
- 显式复制构造函数编译错误
- 使用SSE内部函数编译一个简单的c++程序
- 函数编译时错误
- 模板函数编译错误
- C++:使用类型名作为基的模板类调用函数编译时错误
- 将 MATLAB 函数编译成可以在 linux 终端上运行的东西,w.out MATLAB
- boost::绑定不要使用成员模板函数编译
- C++函数编译错误
- 加速模板函数编译
- 如何在C++中使用辅助函数编译以下flex文件
- 如何让g++使用move构造函数编译c++11代码
- 同时将一组函数编译为.LIB和.DLL