在C ++中使用默认参数跳过模板参数真的不可能,为什么语法建议不? [英] Is it really impossible to skip template parameters with default arguments in C++, why does syntax suggest otherwise?

查看:254
本文介绍了在C ++中使用默认参数跳过模板参数真的不可能,为什么语法建议不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一种方法来跳过一个模板参数,该模板参数不在模板参数列表的末尾,在一个派生类中,该派生类在其基类中被赋予一个默认值。



我做了一些关于这个话题的研究,也在这里。虽然已经讨论了类似的问题SO - 许多答案基本上表明它不工作与非常特殊的情况下像散列图案例这里。另外,我发现了Potatoswatter的这个答案,在我看来,这违反了不可能跳过这样的参数。在他的回答中,他声明这个声明是有效的:

  template<类A,类B = int,类C> 
class X;

假设模板参数不能被跳过(除非在参数列表的末尾)这样的宣言根本没有意义。因为B被分配了一个默认值,但后面是没有默认值的C,在这种情况下,值B总是必须被明确地赋值,使B的默认赋值int完全无用。唯一的情况是,以上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描述的情况下可用于声明:


可以使用的模板参数是通过从
合并模板的所有先前声明的默认参数以与默认函数参数(8.3.6)相同的方式获得的。 [示例

 模板< class T1,class T2 = int& A类; 
template< class T1 = int,class T2> A类;

相当于

  template< class T1 = int,class T2 = int> A类; 

- end example ]


< blocksquote>

I've been trying to find a way to skip a template parameter not located at the end of the template parameter list, in a derived class that has been assigned a default in its base class.

I've done some research on this topic, also here on SO. While similar questions have been discussed on SO - many answers basically suggesting it doesn't work were related to very special cases like the hash map case here. Also I found this answer by "Potatoswatter", which in my opinion contradicts the impossibility of skipping such a parameter. In his answer he claims this declaration would be valid:

template< class A, class B = int, class C >
class X;

Assuming it is true that a template parameter may not be skipped (unless at the end of the argument list) such a declaration would make no sense at all. Since B is assigned a default value, but followed by C which has no default, in this case value B would always have to be assigned explicitly, rendering the assignment of int as default for B completely useless. The only scenario where the declaration of X above would make sense is one where one of the following declarations of Y would be valid:

class Y : public X<double, , const std::string&> { ... }

class Y : public X<A = double, C = const std::string&> { ... }

So is it really impossible to skip a template parameter that is not located at the end of the template parameter list when deriving a specialized class?

If it is impossible why so, and why does legal syntax apparently suggest otherwise (see class X example above)?

If it is in fact not impossible, how can one skip a template argument that has been assigned a default?

解决方案

The relevant standardese is contained in "Template Parameters [temp.param]" (14.1).

Essentially, a default argument may only be used if the parameter to which it applies is not followed by any non-pack parameters that do not have default arguments ([temp.param]/11). However, the syntax you quoted is usable in a declaration in the situation described by [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 ++中使用默认参数跳过模板参数真的不可能,为什么语法建议不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆