简体   繁体   English

我可以在std :: function模板签名类型参数中使用命名参数吗?

[英]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 parameters-and-qualifiers trailing-return-type noptr-declarator 参数和限定符 trailing-return-type

ptr-declarator : ptr声明者

  • noptr-declarator noptr声明符
  • ptr-operator ptr-declarator ptr-operator ptr-declarator

noptr-declarator : noptr-declarator

  • declarator-id attribute-specifier-seq (opt) declarator-id attribute-specifier-seq (opt)
  • noptr-declarator parameters-and-qualifiers 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抽象说明符
  • noptr-abstract-declarator (opt) parameters-and-qualifiers trailing-return-type noptr-abstract-declarator (opt) 参数和限定符 trailing-return-type
  • abstract-pack-declarator 抽象包装说明符

ptr-abstract-declarator : ptr-abstract-declarator

  • noptr-abstract-declarator noptr抽象说明符
  • ptr-operator ptr-abstract-declarator (opt) ptr-operator ptr-abstract-declarator (opt)

noptr-abstract-declarator : noptr-abstract-declarator

  • noptr-abstract-declarator (opt) parameters-and-qualifiers noptr-abstract-declarator (opt) 参数和限定符
  • noptr-abstract-declarator (opt) [ 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.

相关问题 std :: function的模板参数(签名)不是其类型的一部分吗? - Isn't the template argument (the signature) of std::function part of its type? 模板参数和它自己的模板参数都包含同名的类型; 如何在函数签名中引用第二个? - Template argument and its own template argument both contain identically-named types; how can I reference the second in a function signature? 如何使用模板模板类型作为函数参数? - How can I use template template type as an function argument? 模板参数和std :: function参数的推导 - Template argument and deduction of std::function parameters 我可以在std :: function中使用模板类型T来传递lambda类型函数C ++ 11吗? - Can I use template type T in std::function to pass a lambda type function C++11? 我可以制作一个 function 根据模板参数返回不同的类型,而不使用 function 参数吗? - Can I make a function that returns a different type depending on a template argument, without using function parameters? 从 std::function 调用签名推导出模板参数 - Deduce template argument from std::function call signature 使用其他模板类型参数作为类型别名声明以在函数的签名中使用 - Using additional template type parameters as type alias declarations to use in function's signature 如何使用带有模板参数的std :: function - How to use std::function with a template argument 我可以使用 std::string* argv 作为主要 function 参数吗? - Can I use std::string* argv as main function argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM