我有这个代码,我的期望是会有两个不同版本的运算符()
根据模板参数的类型。为什么enable_if在这里工作?
#include <string>
#include <type_traits>
template<typename T>
struct Impl
{
std::enable_if_t<!std::is_pointer<T>::value,T> operator()(const std::string& key, int node)
{
return static_cast<T>();
}
std::enable_if_t<std::is_pointer<T>::value,T> operator()(const std::string& key, int node)
{
return new T();
}
};
int main()
{
}
相反,我得到一个错误的编译: 'std::enable_if_t<std::is_pointer<_Tp>::value, T> Impl<T>::operator()(const string&, int)' cannot be overloaded with 'std::enable_if_t<(! std::is_pointer<_Tp>::value), T> Impl<T>::operator()(const string&, int)'
Nit Pick:什么是'static_cast();'? –
WhiZTiM
@WhiZTiM [ftfy](http://coliru.stacked-crooked.com/a/4418f30d119f86fe) –