Is it possible to ignore the trailing return type feature of c++11 in favor of the function return type deduction feature of c++14?

独自空忆成欢 提交于 2019-12-05 00:17:08

There are three important differences between a function using automatic-return-type-deduction and one with an explicit return-type (even if that is computed):

  1. You cannot do SFINAE on the computability of the return-type if you do not explicitly specify it: You get a hard error instead. Why? Because SFINAE only works with the declaration, not the definition of functions (SFINAE: (template-argument) substitution-failure is not an error).

    automatic-return-type-deduction, no SFINAE
    SFINAE, but no automatic return-type deduction

    #include <iostream>
    int doit(int x, ...) { return x; }
    template<class X, class Y> auto doit(X x, Y y)
    #ifdef TRAILING_RETURN_TYPE
        -> decltype(doit(x) + doit(y))
    #endif
    { return doit(x) + doit(y); }
    int main() {
        std::cout << doit(1, nullptr) << std::endl;
    }
    
  2. At the moment, you cannot forward-declare a function with its actual return-type, if the definition uses automatic return-type-deduction, nor can it be virtual. (Explicit rule)

    7.1.6.4 auto specifier [dcl.spec.auto]

    13 Redeclarations or specializations of a function or function template with a declared return type that uses a placeholder type shall also use that placeholder, not a deduced type.
    14 A function declared with a return type that uses a placeholder type shall not be virtual (10.3).

  3. Only functions with automatic return-type deduction can return a lambda, as there is no other way to get its type.

    auto foo() { return [] {}; }
    

Link to the proposal, which was incorporated into the draft for C++1y:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3638.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!