Correctly propagating a `decltype(auto)` variable from a function

That’s the simplest and most clear way to write it: template <typename F> auto invoke_log_return(F&& f) { auto result = f(); std::printf(” …logging here… %s\n”, result.foo()); return result; } The GCC gets the right (no needless copies or moves) expected result: s() in main prvalue s() …logging here… Foo! lvalue s(const s&) …logging here… Foo! … Read more

Does auto return type deduction force multiple functions to have the same return type?

Based on the following, GCC has the right behaviour in this case, but only by coincidence (see below): ยง7.1.6.4 [dcl.spec.auto]/8 If the init-declarator-list contains more than one init-declarator, they shall all form declarations of variables. Why only by coincidence? The error message is a clue. Changing the functions to deduce the same return type causes … Read more

Use of ‘auto func(int)’ before deduction of ‘auto’ in C++14

This is [dcl.spec.auto/11]: If the type of an entity with an undeduced placeholder type is needed to determine the type of an expression, the program is ill-formed. Once a non-discarded return statement has been seen in a function, however, the return type deduced from that statement can be used in the rest of the function, … Read more