Reputation: 263
Consider a template class
template<class T>
class Foo
{
};
for which I can write a simple specialisation
template<>
class Foo<int>
{
};
I have a situation where I want to specialise Foo with a template class, in detail with a bool which serves as a compile-time flag:
template<>
class Foo<int, bool> // Clearly not the correct notation.
{
}
Uses would include Foo<1, true> and Foo<1, false>.
What is the correct notation for the class name, where I've marked "Clearly not the correct notation."?
I code to the C++11 standard.
Upvotes: 0
Views: 94
Reputation: 781
Seems like default value for template argument.
template<class T, bool flag = false>
class Foo
{
};
template<>
class Foo<int>
{
//"false" specialization (default)
};
template<>
class Foo<int, true>
{
//"true" specialization
};
Upvotes: 1
Reputation: 180575
You need to change the primary template to
template<class T, bool B>
class Foo
{
};
and then you can specialize it like
template<>
class Foo<int, true>
{
};
template<>
class Foo<int, false>
{
};
...
and then you would use it like
Foo<int, true> FooT;
Foo<int, false> FooF;
If you are going to use values for the first parameter like
Foo<1, true>
Then the primary template should be
template<int I, bool B>
class Foo
{
};
and then you can specialize it like
template<>
class Foo<1, true>
{
};
template<>
class Foo<1, false>
{
};
...
Upvotes: 5
Reputation: 62563
This is not directly possible. Your template wants a single parameter, you can't specialize it for two. However, you can (partially) specialize it for some other type which is a template of two parameters.
Example:
template<class T>
class Foo;
template<int, bool> class tag;
template<int>
class Foo<tag<int, true>> { ... };
template<int>
class Foo<tag<int, false>> { ... };
And than you can use it
Foo<tag<1, true>> foo;
Upvotes: 4