[英]Can I use named parameters in std::function template signature type argument?
Can I legally use names for template parameters in std::function
(or another similar construct)? 我可以合法地在std::function
(或其他类似的构造)中使用模板参数的名称吗? Eg given the code 例如给出代码
std::function<int(int, int)> some_func;
Can I rewrite it in following way? 我可以按照以下方式重写吗?
std::function<int(int begin, int end)> some_func;
It would be very nice, if it is standard compliant, because the type alone carries little information about the purpose of the parameter. 如果它符合标准,那将是非常好的,因为单独的类型几乎没有关于参数目的的信息。
So far I've tested it in Visual Studio 2013 and it works. 到目前为止,我已经在Visual Studio 2013中对它进行了测试,但它确实有效。 Edit: It works with gcc 4.8.1 too. 编辑:它也适用于gcc 4.8.1。
Yes, you can provide the parameter name. 是的,您可以提供参数名称。 From paragraph §8.3.5/11 of the C++11 Standard: 根据C ++ 11标准的第8.3.5 / 11段:
An identifier can optionally be provided as a parameter name; 可选地,可以提供标识符作为参数名称;
The same goes for pointer-to-function and pointer-to-member function type. 指向函数的指针和指向成员的函数类型也是如此。 Personally, I would use the identifier only if it expresses more clearly the intention of your code. 就个人而言,只有当它更清楚地表达代码的意图时,才会使用标识符。
For the language lawyers: 对于语言律师:
The grammar does allow this. 语法确实允许这样做。 Whether or not this is intentional is unclear to me. 我是否也不清楚这是否是故意的。 A declarator and an abstract-declarator differ in that the latter doesn't allow a name at the top level. 声明者和抽象声明者的不同之处在于后者不允许顶级名称。
declarator : 声明者 :
ptr-declarator : ptr声明者 :
noptr-declarator : noptr-declarator :
[
constant-expression (opt) ]
attribute-specifier-seq (opt) noptr-declarator [
constant-expression (opt) ]
attribute-specifier-seq (opt) (
ptr-declarator )
(
ptr声明者 )
Note that in a declarator you can have a declarator-id , that is, a name. 请注意,在声明符中,您可以拥有声明者ID ,即名称。 Now consider the syntax of an abstract-declarator : 现在考虑一个abstract-declarator的语法:
abstract-declarator : abstract-declarator :
ptr-abstract-declarator : ptr-abstract-declarator :
noptr-abstract-declarator : noptr-abstract-declarator :
[
constant-expression (opt) ]
attribute-specifier-seq (opt) noptr-abstract-declarator (opt) [
constant-expression (opt) ]
attribute-specifier-seq (opt) (
ptr-abstract-declarator )
(
ptr-abstract-declarator )
An abstract-declarator can't contain a declarator-id at top level. abstract-declarator不能在顶层包含declarator-id 。 However, notice that the parameters-and-qualifiers that occurs in the production rules for abstract-declarator and noptr-abstract-declarator is the same as that in declarator and noptr-declarator . 然而,会注意到发生于抽象声明符和noptr抽象说明符的产生式规则的参数和-限定符是一样的,在说明符和noptr说明符 。 There is no special abstract-parameters-and-qualifiers to enforce not having a name within a parameter list that occurs in an abstract-declarator . 没有特殊的abstract-parameters-and-qualifiers来强制在abstract-declarator中出现的参数列表中没有名称。 So the grammar allows names to be specified for function parameters inside abstract-declarators even though the overall function type can't be given a name. 因此语法允许为abstract-declarators中的函数参数指定名称,即使整个函数类型不能给出名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.