简体   繁体   中英

How do I specialize my class template to fewer arguments

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?

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

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> {};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM