我有一个函数来确定模板类型是否为指针.
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;
}
Run Code Online (Sandbox Code Playgroud)
我有一个初始化函数.
template<class T>
void initialize(T &val) {
if (is_pointer(val))
val = NULL;
else
val = T();
}
Run Code Online (Sandbox Code Playgroud)
显然,当T是string,此代码不能被编译.有没有一种方法可以val = NULL在T指针类型时编译并val = T()在T不是指针类型时编译?