【发布时间】:2016-01-11 00:07:48
【问题描述】:
一个类:
template<typename C, typename T>
class A
{
template <typename U>
class Nested{};
Nested<T> n;
};
我想专攻Nested。这是我尝试过的:
template<typename C, typename T>
class A
{
template <typename U>
class Nested{};
template <>
class Nested<int>{}; // by my logic this should work by I have a compilation error "explicit specialization in non-namespace scope 'class A<C, T>'"
Nested<T> n;
};
我的下一次尝试:
template<typename C, typename T>
class A
{
template <typename U>
class Nested{};
Nested<T> n;
};
template<>
A<>::Nested<int>{}; // What is the correct syntax to do it here? Now I have an error "wrong number of template arguments (0, should be 2)"
在stackoverflow上我找到了一个解决方案:
template<typename C, typename T>
class A
{
template <typename U, bool Dummy = true>
class Nested{}; // why need of this Dummy??
template <bool Dummy>
class Nested<int, Dummy>{}; // why need to provide an argument??
Nested<T> n;
};
它完美地工作,但我不明白如何。为什么要提供一个虚拟模板参数?为什么我不能使用原始专业化 template<> class Nested<int, true>{} 或 template<> class Nested<int>{}?
【问题讨论】:
标签: c++ templates template-specialization