0

Is it possible to catch only classes that have a specific template argument, i.e., something like this:

template< typename T >
void foo( T<int> )
{
  // do something
}

1 Answer 1

4

Yes, but you have to use a template template argument

template <template <typename> class T>
void foo( T<int> )
{
  // do something
}

You can also write

template <typename...> class T

to intercept a type T that receive zero or more type parameters (example: to intercept std::vector that receive two type where the second is with a default value).

Sign up to request clarification or add additional context in comments.

1 Comment

@abraham_hilbert - answer improved; I suggest you to search "template template c++" with google because is an important issue in modern c++; the typename (you can also use class, but I think typename is more clear) inside < > say that T is a template class with a typename argument; by example if you write template <typename, std::size_t> class A, you ask for a template class A that need a type and a std::size_t value as template argument (like std::array).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.