在 C++11 函数中使用尾随返回类型的优势 [英] Advantage of using trailing return type in C++11 functions

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

问题描述

在 C++11 中指定尾随返回类型与普通返回类型相比有什么优势?在此处查看 foo1foo2:

What is the advantage of specifying a trailing return type in C++11, as opposed to a normal return type? Look at foo1 vs foo2 here:

int foo1() {
    return 1;    
}

auto foo2() -> int {
    return 1;    
}

int main() {
    foo1();
    foo2();
}

推荐答案

在这个例子中,它们的意思完全相同.

In this example, they mean the exact same thing.

但是,一致地使用尾随返回类型形式有一些优点(Phil Nash 称这些形式为 东端函数",因为返回类型在东端).

However, there are a few advantages to using the trailing return type form consistently (Phil Nash calls these "East End Functions", since the return type is on the east end).

  1. 使用参数.显然,在使用参数确定返回类型时,必须使用尾随返回类型.

 template <typename T>
 auto print(T const& t) -> decltype(std::cout << t) { return std::cout << t; }

  • 名称查找.在尾随返回类型中,名称查找包括成员函数定义的类范围.这意味着如果要返回嵌套类,则不必重新键入类:

  • Name lookup. In a trailing return type, name lookup includes the class scope for member function definitions. This means you don't have to retype the class if you want to return a nested class:

     Type C::foo() { ... }         // error: don't know what Type is
     C::Type C::foo() { ... }      // ok
    
     auto C::foo() -> Type { ... } // ok
    

  •  D ::C::foo() { ... }         // error, parsed as D::C::foo() { ... }
    
     auto ::C::foo() -> D { ... } // ok
    

    在某些情况下 trailing-return-type 是强制性的,在某些情况下它会有所帮助,并且在某些情况下它会做同样的事情.没有比单纯的字符数更糟糕的情况.

    There are cases where trailing-return-type is mandatory, there are cases where it is helpful, and there are cases where it does the same thing. There are not cases where it is worse for reasons other than simply character count.

    另外,在数学上我们习惯于将函数视为 A ->B 而不是 B(A),所以 auto(*)(A) ->B 作为一个函数指针,接受一个 A 并返回一个 BB(*)(A).

    Plus, mathemtically we're used to thinking of functions as A -> B and not so much B(A), and so auto(*)(A) -> B as a function pointer taking an A and returning a B is a little closer to that view than B(*)(A).

    另一方面,编写 auto main() ->int 看起来很可笑.

    On the other hand, writing auto main() -> int looks ridiculous.

    但老实说,这主要是因为不熟悉.它本身并没有什么可笑的.如果有的话,有点不幸的是语言在这里使用 auto 作为声明函数的方式——不是因为它太长(即一些其他语言使用 fun 甚至 fn) - 但因为它与 auto 的其他用法并没有真正的区别.如果换成 func,我认为会更好(虽然现在改变没有任何意义).

    But honestly, that's mostly because of unfamiliarity. There's nothing inherently ridiculous about it. If anything, it's a bit unfortunate that the language uses auto here as a way to declare a function - not because it's too long (i.e. some other languages use fun or even fn) - but because it's not really distinct from other uses of auto. If it were func instead, I think it would have been better (although it would not make any sense to change now).

    最终,这纯粹是基于意见的.只需编写有效的代码.

    Ultimately, this is purely opinion based. Just write code that works.

    这篇关于在 C++11 函数中使用尾随返回类型的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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