c++ - Overriding a virtual method in a base class with conditional type traits in derived template class -


can explain why code below gives error "error c2259: 'propertyvalue': cannot instantiate abstract class" in visual studio 2015 c++?

is compiler not able identify conditionally specified function converttodevice() in derived class propertyvalue has same signature?

many thanks,

john

#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] not duplicate question because of conditional traits aspect.

first, declare function you're trying override templates. cannot have template virtual function. it's simple that.

for solution, seem have made templates able switch between 2 implementation. simple solution implement single function overrides, , call template function in it:

template<typename t> struct propertyvalue : basepropertyvalue {     t value;      // simpler constructor     propertyvalue(t val) : value{std::move(val)} {}      // override keyword important     int converttodevice(void** ptrdobject) override     {         return converttodeviceimpl(ptrdobject);     }  private:     template<class q = t>     typename std::enable_if<!std::is_pointer<q>::value, int>::type     converttodeviceimpl(void** ptrdobject)     {         return 1;     }      template<class q = t>     typename std::enable_if<std::is_pointer<q>::value, int>::type     converttodeviceimpl(void** ptrdobject)     {         return 2;     } }; 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -