尾随返回类型语法样式是否应该成为新 C++11 程序的默认样式? [英] Should the trailing return type syntax style become the default for new C++11 programs?

查看:38
本文介绍了尾随返回类型语法样式是否应该成为新 C++11 程序的默认样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++11 支持新的函数语法:

C++11 supports a new function syntax:

auto func_name(int x, int y) -> int;

目前这个函数将被声明为:

Currently this function would be declared as:

int func_name(int x, int y);

新样式似乎还没有被广泛采用(比如在 gcc stl 中)

The new style does not seem to be widely adopted yet (say in the gcc stl)

但是,这种新风格是应该在新的 C++11 程序中处处首选,还是只在需要时使用?

However, should this new style be preferred everywhere in new C++11-programs, or will it only be used when needed?

就我个人而言,如果可能的话,我更喜欢旧样式,但混合样式的代码库看起来很丑陋.

Personally, I prefer the old style when possible, but a code-base with mixed styles looks pretty ugly.

推荐答案

在某些情况下,您必须使用尾随返回类型.最值得注意的是,如果指定了 lambda 返回类型,则必须通过尾随返回类型指定.此外,如果您的返回类型使用 decltype 要求参数名称在范围内,则必须使用尾随返回类型(但是,通常可以使用 declval> 解决后一个问题).

There are certain cases where you must use a trailing return type. Most notably, a lambda return type, if specified, must be specified via a trailing return type. Also, if your return type utilizes a decltype that requires that the argument names are in scope, a trailing return type must be used (however, one can usually use declval<T> to work around this latter issue).

尾随返回类型确实有一些其他的小优势.例如,考虑使用传统函数语法的非内联成员函数定义:

The trailing return type does have some other minor advantages. For example, consider a non-inline member function definition using the traditional function syntax:

struct my_awesome_type
{
    typedef std::vector<int> integer_sequence;

    integer_sequence get_integers() const;
}; 

my_awesome_type::integer_sequence my_awesome_type::get_integers() const
{
    // ...
}

在类名出现在 ::get_integers 之前之前,成员类型定义不在范围内,因此我们必须重复类限定两次.如果我们使用尾随返回类型,则不需要重复类型名称:

Member typedefs are not in scope until after the name of the class appears before ::get_integers, so we have to repeat the class qualification twice. If we use a trailing return type, we don't need to repeat the name of the type:

auto my_awesome_type::get_integers() const -> integer_sequence
{
    // ...
}

在这个例子中,这没什么大不了的,但是如果你有很长的类名或者类模板的成员函数没有内联定义,那么它可以在可读性上产生很大的不同.

In this example, it's not such a big deal, but if you have long class names or member functions of class templates that are not defined inline, then it can make a big difference in readability.

在 C++Now 的 Fresh Paint" 会议中2012 年,Alisdair Meredith 指出,如果您始终如一地使用尾随返回类型,那么您所有函数的名称都会整齐排列:

In his "Fresh Paint" session at C++Now 2012, Alisdair Meredith pointed out that if you use trailing return types consistently, the names of all of your functions line up neatly:

auto foo() -> int;
auto bar() -> really_long_typedef_name;

我在 CxxReflect 中到处都使用了尾随返回类型,所以如果您正在寻找一个代码如何一致使用它们的示例,您可以在那里查看(例如,type).

I've used trailing return types everywhere in CxxReflect, so if you're looking for an example of how code looks using them consistently, you can take a look there (e.g, the type class).

这篇关于尾随返回类型语法样式是否应该成为新 C++11 程序的默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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