Question

I have a class template I am trying to specialize for five different cases:

template<size_t Number, typename T>
class Foo {};

template<typename T>
class Foo {}; // I get template argument errors here

template<size_t Number, typename T, size_t Size>
class Foo<Number, T[Size]> {};

template<size_t Number, typename T>
class Foo<Number, T[]> {};

template<typename T>
class Foo<T[]> {};  // I get template argument errors here too

Is this possible?

Was it helpful?

Solution 3

I ended up with this interface, where a Number of -1 fails at compile time based on an implementation detail in my class. Such that you must specify a valid Number.

template<typename T, size_t Number = -1>
class Foo {};

template<typename T>
class Foo<T, -1> {};

template<typename T, size_t Size, size_t Number>
class Foo<T[Size], Number> {};

template<typename T, size_t Number>
class Foo<T[], Number> {};

template<typename T>
class Foo<T[], -1> {};

OTHER TIPS

No. Instead, you can create a partial specialization. For the first attempted specialization, what do you want for Number? Then fill it in: template <typename T> class Foo<3, T> {};

I think this should help you... http://www.cplusplus.com/doc/tutorial/templates/ you can see the template specification section and get a clear example..

Hope that help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top