0

I have a template class (A). How can I force template argument to have an specific function?

template <class T>
class A 
{

} ;

T should have specific function.

Thanks

Georg Fritzsche
  • 93,086
  • 26
  • 183
  • 232
AmirC
  • 188
  • 3
  • 14
  • C++ is duck-typed. So just assume it has that function, and if it doesn't the compiler will tell them the `A` is missing function `blahblah` anyway. – Mooing Duck Jan 27 '12 at 23:54

1 Answers1

1

If your template code uses the function, compilation will already fail if T doesn't have it.

But if your goal is to provide clearer error messages to the users of A, you can use static asserts based on checks on T having that member. Note however that you have to watch out for inherited functions.

Community
  • 1
  • 1
Georg Fritzsche
  • 93,086
  • 26
  • 183
  • 232