网络科技

    今日:941| 主题:246502
收藏本版
互联网、科技极客的综合动态。

[其他] Trailing return types everywhere

[复制链接]
容颜出现编译错误 发表于 2016-12-1 03:58:56
117 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Trailing return types are an oddity in C++ – we should use them only when necessary.  
  A few days ago one of my coworkers asked me to explain an    odd line of codehe had encountered in an open source library. The line was similar to this:  
  1. auto getMulticastHops() const -> int;
复制代码
Some people will know that this is a way of declaring functions that came into the language with C++11. The part    -> intis called “trailing return type”, and the line is exactly the same as  
  1. int getMulticastHops() const;
复制代码
    Why have trailing return types?  

  There were good reasons to introduce trailing return types in C++11. The standard examples are function templates where the return type depends on the argument types. Let’s, for example, take a function that multiplies two values:
  1. template<class T, class U>
  2. auto multiply(T const& lhs, U const& rhs) -> decltype(lhs * rhs) {
  3.   return lhs * rhs;
  4. }
复制代码
One could, of course, use    std::declvalto not have to use trailing return types:  
  1. template<class T, class U>
  2. decltype(std::declval<T>() * std::declval<U>()) multiply(T const& lhs, U const& rhs) {
  3.   return lhs * rhs;
  4. }
复制代码
As you see, this gets cluttered and barely readable very quickly. Another example is lambdas, where the “normal” way to declare return types is not allowed by the syntax.
      What about return type deduction?  

  C++11 lambdas already had return type deduction for the easier cases. C++14 added it for the more general cases and for normal functions as well. The above function can be written simply like this:
  1. template<class T, class U>
  2. auto multiply(T const& lhs, U const& rhs) {
  3.   return lhs * rhs;
  4. }
复制代码
Return type deduction can help in many cases where trailing return types were necessary before, but not everywhere. For example, the compiler will always deduce return by value, never by reference. So if you want to return by reference from a lambda, there is no way around using trailing return types.
  Other cases where return type deduction is impossible are of course function declarations without a body – these will however never be lambdas.
      Trailing return types everywhere?  

  It might be tempting to use trailing return types everywhere now. Some people also argue that they make our code more consistent. Others use them because they have to be used in some places anyway, but many will also use them because they are new and new is cool. We get this a lot with new features and possibilities. They get hyped and sometimes overused.
  I am not saying that we should not decide to switch to “Always Auto for Functions” – but if we do, we should do it for the right reasons. Keep in mind that there also are reasons that speak against that style:
  
       
  • There are billions of lines of old C++ code out there using the classic return type style. Keeping that in mind, “consistency” might be a less compelling argument for trailing return types.   
  • There are lots of programmers out there who are not yet familiar with trailing return types. That may be C++ programmers, but also programmers coming from other languages. Using trailing return types everywhere might pose an additional hurdle for those programmers to get familiar with your code.  
  I have to confess, I am somewhat undecided on the matter. I’ll probably stick to the classic style for the time being and see for which style the community will settle in the next years.
友荐云推荐




上一篇:Instagram Advertisers Switch From Celebrities To "Microinfluencers"
下一篇:Next-gen Gear VR may feature eye and face tracking
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

谁把安心卖给我 发表于 2016-12-1 05:09:23
这么强,支持容颜出现编译错误,佩服
回复 支持 反对

使用道具 举报

ggstone 发表于 2016-12-1 05:19:49
小白一个 顶一下
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表