What is difference between decltype(auto) and decltype(returning expr) as return type?

后端 未结 3 1179
慢半拍i
慢半拍i 2021-02-13 03:48

What is the difference between decltype(auto) and decltype(returning expression) as return type of a function (template) if expr used with

3条回答
  •  执念已碎
    2021-02-13 04:38

    Yet another example. In the decltype(auto) case, lambdas are allowed. In the decltype(expr) case, lambdas are disallowed.

    That's because a) lambdas are not allowed as unevaluated operands (such as in sizeof or decltype) and b) the type in the decltype and in the body of the two expressions would be different, since they are two distinct lambda expressions

    // ill-formed: lambda within decltype
    auto f() -> decltype([]{}) { return []{}; };
    
    // allowed: lambda is not in decltype, and only appears once
    auto f() -> decltype(auto) { return []{}; }
    

提交回复
热议问题