2

I'm trying to make a constructor in a templated class act differently if its template parameter is a vector, but am getting an error on the second definition saying "template argument list must match the parameter list". This is what my code looks like:

template<typename T>
class Test
{
    Test();
};

template<typename T>
Test<T>::Test() { /* non-vector stuff */ };

template<typename T>
Test<std::vector<T>>::Test() { /* vector stuff */ };

Does anyone know how to do this?

  • [partial template specialization](https://en.cppreference.com/w/cpp/language/partial_specialization). Related: https://stackoverflow.com/q/72509401/ – Ch3steR Jun 18 '22 at 15:02
  • On top of `if constexpr` mentioned in the first duplicate target, see https://stackoverflow.com/a/12043078/7976805 for "detecting if T is a vector" ([see it online](https://godbolt.org/z/53TY7xqrP)). Also note that this may be not needed if your constructors are overloads of each other (you could enable/disable them with metaprogramming instead of coding logic in `if`). – Yksisarvinen Jun 18 '22 at 15:12
  • You can use `if constexpr` instead of trying to specialize the constructor: https://godbolt.org/z/zx1EnvTvh – Goswin von Brederlow Jun 18 '22 at 15:13

0 Answers0