功能中的自动参数类型 [英] auto parameter type in functions

查看:50
本文介绍了功能中的自动参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道标准委员会是否考虑过扩展C ++ 14 auto关键字来推导函数模板参数类型,就像今天在通用lambda中所存在的那样. (从这个答案中可以很好地看到)

I would like to know if the standard committee considered expanding the C++14 auto keyword to deduce function template parameter type, as it exists today in generic lambdas. (as can be seen nicely depicted in this answer)

因为它可以在lambda函数中使用,所以它也应该可以在任何函数中使用.当然,使用经典语法完全是多余的:

Because it works in lambda functions, it should also work in any function. Of course it would be totally redundant with the classic syntax:

template< typename T >
void f(T param);

但是能够写出同样的结果:

But being able to write this, for the same result:

void f(auto param);

我认为这样可以减少代码的阻塞(缩短整洁的时间),并在此用例中保持高度的一致性:

I think would allow for less clogged code (shorter neater cleaner) and allow a great consistency in this use case:

auto v = func1();
f(v);

如您所见,我们使用了自动类型推断器来声明v,但是随后我们必须使用硬类型的参数化函数f或模板化的f.
auto结合使用时,我们应该使用auto,这样会更加一致.

As you can see, we used the auto type deducer to declare v, but then we have to use either a hard-typed parameterized function f, or a templated f.
In combination with auto we should use auto, that would be more consistent.

这个问题确实有效地询问了同一件事,但没有那么直接.尚未得到user657267给出的答案,我将在下面进行复制和扩展.

this question indeed asks effectively the same thing, but less directly. And has yet to get the answer that user657267 gives, which I reproduce and extend thereunder.

推荐答案

好,因此,感谢Piotr指出了另一个问题,询问了同一件事,我在注释中找到了可以解决此问题的信息,这里是:

Ok, so thanks to Piotr pointing this other question asking about the same thing, I found the information in a comment that will resolve this, here it is:

http://www.open- std.org/jtc1/sc22/wg21/docs/papers/2014/n4040.pdf

第16页的第5.1.1章,名为generic functions

page 16, chapter 5.1.1 named generic functions

泛型函数是一个函数模板,其 template-parameter-list有一个参数声明,其 type-specifier可以是auto或约束类型名称.

A generic function is a function template whose template-parameter-list has a parameterdeclaration whose type-specifier is either auto or a constrained-type-name.

[示例:

auto f(auto x); // Ok
void sort(C& c); // Ok (assuming C names a concept)

—结束示例]

这似乎很积极:)

紧随其后的是明显的措辞,与通用lambda相匹配:

followed by the excpected obvious wording, that matches generic lambda:

泛型函数的声明具有template-parameter-list 每种都由一个发明的类型模板参数组成 自动.

The declaration of a generic function has a template-parameter-list that consists of one invented type template-parameter for each occurrence of auto.

[示例:以下通用函数声明是等效的:

[ Example: The following generic function declarations are equivalent:

template<typenaem T>  
conxtexpr bool C() { ... }  
auto f(auto x, const C& y);  
template<typename T1, C T2> 
auto f(T1 x, const T2& y);

y的类型是C约束的类型参数.—结束示例]

The type of y is a type parameter constrained by C. — end example ]

这篇关于功能中的自动参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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