Search code examples
c++templatesc++14template-templates

Catch classes with specific template argument


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
}

Solution

  • 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).