2021-03-31

Is there a way to specify that a template parameter type must be derived from a particular base class?

Is there a way to specify that a template parameter type must be derived from a particular base class? This is how I wish it would work:

class  foo
{
    a_foo_function();
}
//.......
//.......
//.......
template  < class F : public foo >  // <<<=== my deceptively creative syntax
class bar
{
    F * pfd;
    bar(F* food){ pfd = food; }
    void f() const {  pfd->a_foo_function();  }  //guaranteed to work in my book
}
//.......
class foo                         just_a_plain_foo;
//.......
class foodish : public foo {}     a_foodish;
//.......
class foolish : public not_foo {} a_foolish;
//.......
typedef bar<foodish> barf;
barf b(a_foodish); //ok
barf c(a_foolish); //NOT ok
barf d(just_a_plain_foo); //NOT ok ... pdf pointer is to a derived

Ain't that beautiful? But my beautiful syntax won't compile, though; and I can't seem to find how to do this. Note that using the base class as template parameter won't work for me, as I may have distinct and mutually incompatible classes deriving from foo. TIA.



from Recent Questions - Stack Overflow https://ift.tt/2PGemsT
https://ift.tt/eA8V8J

No comments:

Post a Comment