c++ - Virtual function implemented as template in derived class -


having following code:

struct base {     virtual void print(int x) = 0;     virtual void print(float x) = 0; };  struct derived : public base {     template<typename t>     void print(t x)     {         std::cout<<x<<std::endl;     } }; 

is possible c++ black magic(explicit instantiation types, smart using, etc) recognize implementation of:

virtual void print(int x) = 0; virtual void print(float x) = 0; 

in derived class in form of:

template<typename t> void print(t x) 

no, there isn't.

what can forward local template implementation:

struct derived : public base {     void print(int x) override { printtempl(x); }     void print(float x) override { printtempl(x); }      template <typename t>     void printtempl(t x)     {         std::cout << x << std::endl;     } }; 

if find verbose, , have many such prints, can macro it:

#define print_fwd(typ) void print(typ x) override { printtempl(x); } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -