【发布时间】:2017-09-11 20:35:41
【问题描述】:
谁能解释一下为什么下面的代码会在 Visual Studio 2015 C++ 中给出错误“error C2259: 'PropertyValue': cannot instantiate abstract class”?
编译器是否无法识别派生类PropertyValue中的条件指定函数ConvertToDevice()具有相同的签名?
非常感谢,
约翰
#include <type_traits>
#include <typeinfo>
class BasePropertyValue
{
public:
virtual int ConvertToDevice(void** ptrdObject) = 0;
};
template<typename T> class PropertyValue : public BasePropertyValue
{
public:
T value;
PropertyValue(T val)
{
value = val;
}
template<class Q = T>
typename std::enable_if<!std::is_pointer<Q>::value, int>::type ConvertToDevice(void** ptrdObject)
{
return 1;
}
template<class Q = T>
typename std::enable_if<std::is_pointer<Q>::value, int>::type ConvertToDevice(void** ptrdObject)
{
return 2;
}
};
void main()
{
PropertyValue<double>* prop1 = new PropertyValue<double>(20);
prop1->ConvertToDevice(nullptr);
double x = 20;
PropertyValue<double*>* prop2 = new PropertyValue<double*>(&x);
prop2->ConvertToDevice(nullptr);
return;
}
[edit] 由于条件特征方面,这不是一个重复的问题。
【问题讨论】:
-
一个好的提示是始终声明您打算覆盖的函数
override;如果您实际上没有覆盖任何内容,那么您将遇到编译器错误 -
当你 add the
overridekeyword 时,编译器很好地解释了为什么这不起作用:“成员模板......可能没有 virt-specifiers”。基本上,你不能混合使用虚函数和模板
标签: c++ c++11 templates sfinae typetraits