Forcing a template parameter to be derived from another

From: Gabriele Trombetti (nonvalid_at_nowhere.com)
Date: 03/10/04


Date: Wed, 10 Mar 2004 15:24:05 -0600

Hi all,
Is there a way in C++ to force that a template parameter be derived from
something?

The code below should be clear for everybody. I'm asking that the
template parameter T_or_derived is T or derived publicly from T,
otherwise the compiler should give error. Unfortunately this syntax does
not compile.

Is there a way to enforce this constraint? If not, it seems to me that
this would be a very neat addition to C++.

template<class T>
class something
{
...
   template<class T_or_derived : public T>
   const something<T> & operator =
              (something<T_or_derived> & const copythis) {...}
...
};



Relevant Pages

  • Template class partial specialisation problem
    ... Template parameter is "int" ... Template parameter is "long" ... My compiler throws this out with: ... test.cpp:10: error: parse error before `)' token ...
    (comp.lang.cpp)
  • Re: Template function question
    ... with this code (why the compiler accepts the call to DoSomething2 but ... template parameter 'T' is ambiguous ... // or 'const int &' ... the compiler deduces T to be 'const int&', ...
    (microsoft.public.vc.language)
  • Re: friend function of template
    ... >> template parameter". ... >> This can be compiled under the online compiler Comeau. ... > template <int L, int M, int T> ...
    (microsoft.public.vc.language)
  • Re: Pass a contained class to template?
    ... > Is it possible to pass a contained class as a template parameter? ... As I say, no idea if it works, but if I were writing a compiler to ... > template <class T, class Cont> ...
    (microsoft.public.vc.language)
  • Re: C Strings not returning from a function
    ... A C compiler should refuse to compile it. ... with a char** is a constraint violation. ... C does enforce the same rules. ... types is a constraint violation, ...
    (comp.lang.c)