我可以使用auto或decltype代替尾随返回类型吗? [英] Can i use auto or decltype instead trailing return type?

查看:171
本文介绍了我可以使用auto或decltype代替尾随返回类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现trailing return type非常容易定义返回复杂类型的函数的返回,例如:

I find trailing return type so easy to define the return of a function that returns a complicated types e.g:

auto get_diag(int(&ar)[3][3])->int(&)[3]{ // using trailing return type
    static int diag[3]{
        ar[0][0], ar[1][1], ar[2][2]
    };
    return diag;
}

auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer
    static int diag[3]{
        ar[0][0], ar[1][1], ar[2][2]
    };
    return diag;
}

int main(){

    int a[][3]{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    decltype(get_diag(a)) diag{
        get_diag(a)
    };

    for (auto i : diag)
        std::cout << i << ", ";
    std::cout << std::endl;

    decltype(get_diag2(a)) diag2{
        get_diag2(a)
    };

    for (auto i : diag2)
        std::cout << i << ", ";
    std::cout << std::endl;


    std::cout << std::endl;
}

  • 我想知道功能get_diagget_diag2之间的区别是什么.因此,只要输出是相同的,为什么我需要使用尾随返回类型?
    • I want to know what is the difference between the functions get_diag and get_diag2. So as long as the output is the same why I need to use trailing return type?
    • 推荐答案

      auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer
          static int diag[3]{
              ar[0][0], ar[1][1], ar[2][2]
          };
          return diag;
      }
      

      在C ++ 11编译器中将不起作用.在C ++ 14中添加了不带尾随返回类型的auto用法,其行为类似于auto将变量用于变量时的工作方式.这意味着它将永远不会返回引用类型,因此您必须使用auto&返回对要返回的对象的引用.

      Will not work in a C++11 compiler. Using auto without a trailing return type was added to C++14 and acts like how auto works when using it for a variable. This means it will never return a reference type so you have to use auto& to return a reference to the thing you want to return.

      如果您不知道是否应该返回引用或值(在通用编程中经常发生这种情况),则可以使用decltyp(auto)作为返回类型.例如

      If you do not know if you should return a reference or a value (this happens a lot in generic programming) then you can use decltyp(auto) as the return type. For example

      template<class F, class... Args>
      decltype(auto) Example(F func, Args&&... args) 
      { 
          return func(std::forward<Args>(args)...); 
      }
      

      如果func按值返回,

      将按值返回;如果func返回参考,则按引用返回.

      will return by value if func returns by value and return by reference if func returns a reference.

      简而言之,如果您使用的是C ++ 11,则必须在前面或后面指定返回类型.在C ++ 14及更高版本中,您可以只使用auto/decltype(auto)并让编译器为您处理它.

      In short if you are using C++11 you have to specify the return type, either in front or as a trailing return type. In C++14 and above you can just use auto/decltype(auto) and let the compiler deal with it for you.

      这篇关于我可以使用auto或decltype代替尾随返回类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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