C++ 函数标题中的箭头运算符 (->)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22514855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
arrow operator (->) in function heading
提问by user1234567
I found such code:
我找到了这样的代码:
template <typename T, typename T1> auto compose(T a, T1 b) -> decltype(a + b) {
return a+b;
}
I figured with all details, that were new to me, but one.
Tell me please, where can I read about, what does the arrow operator (->
) mean in function heading?
I guess purely logically, that ->
operator determines a type, that will be gotten by auto
, but I want to get this straight, but can't find information.
我想通了所有细节,这对我来说是新的,但只有一个。请告诉我,我在哪里可以读到,->
函数标题中的箭头运算符 ( ) 是什么意思?我猜纯粹从逻辑上讲,该->
运算符确定了一种类型,该类型将通过 获得auto
,但我想弄清楚这一点,但找不到信息。
回答by Jan Hudec
In C++11, there are two syntaxes for function declaration:
在 C++11 中,函数声明有两种语法:
return-typeidentifier(
argument-declarations...)
返回类型标识符(
参数声明...)
and
和
auto
identifier(
argument-declarations...)
->
return_type
auto
标识符(
参数声明... )
->
return_type
They are equivalent. Now when they are equivalent, why do you ever want to use the latter? Well, C++11 introduced this cool decltype
thing that lets you describe type of an expression. So you might want to derive the return type from the argument types. So you try:
它们是等价的。现在,当它们等效时,您为什么要使用后者?好吧,C++11 引入了这个很酷的decltype
东西,它可以让你描述表达式的类型。因此,您可能希望从参数类型派生返回类型。所以你试试:
template <typename T1, typename T2>
decltype(a + b) compose(T1 a, T2 b);
and the compiler will tell you that it does not know what a
and b
are in the decltype
argument. That is because they are only declared by the argument list.
并且编译器会告诉您它不知道参数中的内容a
和内容。那是因为它们仅由参数列表声明。b
decltype
You could easily work around the problem by using declval
and the template parameters that are already declared. Like:
您可以通过使用declval
和已声明的模板参数轻松解决该问题。喜欢:
template <typename T1, typename T2>
decltype(std::declval<T1>() + std::declval<T2>())
compose(T1 a, T2 b);
except it's getting really verbose now. So the alternate declaration syntax was proposed and implemented and now you can write
除了它现在变得非常冗长。所以替代声明语法被提出并实现,现在你可以写
template <typename T1, typename T2>
auto compose(T1 a, T2 b) -> decltype(a + b);
and it's less verbose and the scoping rules didn't need to change.
它不那么冗长,范围规则也不需要改变。
C++14 update:C++14 also permits just
C++14 更新:C++14 也允许只
auto
identifier(
argument-declarations...)
auto
标识符(
参数声明...)
as long as the function is fully defined before use and all return
statements deduce to the same type. The ->
syntax remains useful for public functions (declared in the header) if you want to hide the body in the source file. Somewhat obviously that can't be done with templates, but there are some concrete types (usually derived via template metaprogramming) that are hard to write otherwise.
只要函数在使用前完全定义并且所有return
语句都推导出相同的类型。->
如果您想隐藏源文件中的主体,该语法对于公共函数(在标题中声明)仍然有用。显然,模板无法做到这一点,但有一些具体类型(通常通过模板元编程派生)很难以其他方式编写。
回答by murrekatt
In plain english it tells that the return type is the inferred type of the sum of a
and b
.
通俗地说,它告诉我们返回类型是a
and之和的推断类型b
。