为什么在C ++ 11中需要添加尾随返回类型? [英] Why was the addition of trailing-return-types necessary in C++11?

查看:130
本文介绍了为什么在C ++ 11中需要添加尾随返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于开始阅读,我不明白为什么需要尾随返回类型.

I've finally started to read up on c++11 and I fail to understand why trailing-return-types are required.

我遇到了以下示例,该示例用于突出显示该问题:

I came across the following example, which is used to highlight the problem:

template<class Lhs, class Rhs>
  decltype(lhs+rhs) adding_func(const Lhs &lhs, const Rhs &rhs) {return lhs + rhs;} 

该示例是非法的,因为decltype(lhs+rhs)不起作用,因为标识符lhsrhs仅在解析阶段之后有效.

The example is illegal, because decltype(lhs+rhs) does not work, since the identifiers lhs and rhs are only valid after the parsing phase.

我想我的问题是关于decltype类型解析的时间.如果我没记错的话,关键字decltype用于在编译时确定表达式的类型.

I guess my question is about the timing of decltype type resolution. If I am not mistaken, the keyword decltype is used to determine the type of an expression at compile-time.

在所有解析完成后,我看不到让decltype执行类型解析的不利之处(对于上面的示例来说,这很好用).我相信这是解决问题的一种更简单的方法...

I fail to see a downside to having decltype perform type resolution after all parsing is completed (which would work fine for the above example). I believe this would have been a simpler way to solve the problem...

相反,C ++ 11标准提供了尾随返回类型:

Instead, the C++11 standard provides trailing-return-types:

template<class Lhs, class Rhs>
  auto adding_func(const Lhs &lhs, const Rhs &rhs) -> decltype(lhs+rhs) {return lhs + rhs;}

毫无疑问,我缺少什么,因为我看不到尾随返回类型的其他用法.我的推理的缺陷在哪里?

I have no doubt that I am missing something, since I fail to see the other use of trailing-return-types. Where is the flaw in my reasoning?

对我来说,尾随返回类型似乎是一个过于复杂的解决方案,因为在解析整个函数体之后具有decltype类型的分辨率也可以工作吗?

The trailing-return-types seem like an overly complex solution to me since having decltype type resolution after parsing the full function body would work just as well?

推荐答案

在所有解析完成后,我看不到让decltype执行类型解析的弊端(对于上面的示例来说,这样做很好).

I fail to see a downside to having decltype perform type resolution after all parsing is completed (which would work fine for the above example).

不利之处在于,如果不从根本上改变C ++解析和处理模型的基本基础,这是不可能的.

The downside is that it's not possible without fundamentally altering the basic foundations of the C++ parsing and processing model.

为了执行您建议的操作,编译器将必须查看decltype语法并对该语法的内容进行一些基本的词法分析.然后,它继续解析更多的源文件.在稍后的某个时刻(何时?),它决定执行以下操作:嘿,我之前看过的东西?我现在将为他们进行所有解析工作."

In order to do what you suggest, the compiler will have to see the decltype syntax and do some basic lexical analysis of the contents of the syntax. Then, it goes on to parse more of the source file. At some later point (when?), it decides to go, "hey, that stuff I looked at before? I'm going to do all of the parsing work for them now."

作为一般规则,C ++不支持在符号前定义 . C ++解析框架的基本假设是,如果在使用符号之前未声明该符号,则它是编译器错误.

As a general rule, C++ doesn't support looking ahead for the definition of symbols. The basic assumption of the C++ parsing framework is that, if the symbol is not declared before it is used, it is a compiler error.

班级可以先行一步,但仅限于其成员.这部分是因为它很清楚id表达式何时可以引用成员变量(即:如果它不引用作用域中已声明的局部变量或全局变量).这里不是这种情况,我们不确定id-expression到底指的是什么.

Classes can get away with lookahead, but only with respect to their members. This is in part because its quite clear when an id-expression could be referring to a member variable (ie: if it's not referring to an already declared local or global variable in scope). That's not the case here, where we're not sure what exactly the id-expression could be referring to.

此外,您的建议还会造成歧义.这是什么意思:

Furthermore, what you suggest creates ambiguities. What does this mean:

int lhs;

template<class Lhs, class Rhs>
  decltype(lhs+rhs) adding_func(const Lhs &lhs, const Rhs &rhs);

decltype语法是指全局变量lhs还是局部变量lhs?

Is the decltype syntax refering to the global lhs variable, or the local lhs function parameter?

我们现在的操作方式,这两者之间有明确的界限:

The way we do it now, there's a clear delineation between these two:

int lhs;
float rhs;

template<class Lhs, class Rhs>
  decltype(lhs+rhs) adding_func1(const Lhs &lhs, const Rhs &rhs);
template<class Lhs, class Rhs>
  auto adding_func2(const Lhs &lhs, const Rhs &rhs) -> decltype(lhs+rhs);

adding_func1是指全局变量. adding_func2指的是功能参数.

adding_func1 refers to the global variables. adding_func2 refers to the function parameter.

因此,您可以从根本上破坏每个C ++编译器.或者,您也可以稍后指定返回类型.

So you can either radically break every C++ compiler on the face of the Earth. Or you can simply late-specify your return type.

或者您可以采用C ++ 14方法,而完全说明.

Or you can take the C++14 approach and not bother to state it at all.

这篇关于为什么在C ++ 11中需要添加尾随返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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