我一直在尝试寻找一种方法来跳过不位于模板参数列表末尾的模板参数,在派生类中,该派生类已在其基类中分配了默认值。
我已经对这个主题做了一些研究,也在 SO 上。虽然在 SO 上讨论了类似的问题 - 许多答案基本上表明它不起作用与非常特殊的情况有关,例如 HashMap 情况 here .我还发现了 this由“Potatoswatter”回答,我认为这与跳过此类参数的可能性相矛盾。在他的回答中,他声称该声明是有效的:
template< class A, class B = int, class C >
class X;
假设模板参数确实不能被跳过(除非在参数列表的末尾),这样的声明将毫无意义。由于 B 被分配了一个默认值,但后面跟着没有默认值的 C,在这种情况下,值 B 将始终必须显式分配,从而使 int 的分配作为 B 的默认值完全无用。上述 X 声明唯一有意义的情况是以下 Y 声明之一有效:
class Y : public X<double, , const std::string&> { ... }
class Y : public X<A = double, C = const std::string&> { ... }
那么在派生特化类的时候真的不能跳过一个不在模板参数列表末尾的模板参数吗?
如果这是不可能的,为什么会这样,为什么法律语法显然另有建议(参见上面的 X 类示例)?
如果事实上并非不可能,那么如何跳过已分配默认值的模板参数?
最佳答案
相关标准包含在“模板参数[temp.param]”(14.1)中。
本质上,只有在应用默认参数的参数后面没有任何没有默认参数的非包参数 ([temp.param]/11) 时,才可以使用默认参数。但是,在 [temp.param]/10 描述的情况下,您引用的语法可用于声明:
The set of default template-arguments available for use is obtained by merging the default arguments from all prior declarations of the template in the same way default function arguments are (8.3.6). [Example:
template<class T1, class T2 = int> class A; template<class T1 = int, class T2> class A;
is equivalent to
template<class T1 = int, class T2 = int> class A;
— end example]
关于c++ - 在 C++ 中使用默认参数跳过模板参数真的不可能吗,为什么语法另有建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32414867/