c++ - How to allow template class instantiation only if the type T derives from type X? -


template <typename t> class test { };  class : public x;  class b;  test<a> a; // ok test<b> b; // not ok 

i accomplish this.

maybe can accomplished more easily. basically, need this: template class t should able lock std::mutex member m_mutex in object of type t if exists.

with static assertion , appropriate type trait class:

#include <type_traits>  template <typename t> class test {     static_assert( std::is_base_of<x,t>::value, "t doesn't derive x!");  }; 

live example.


Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -