Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decltype deducted result of in-class defined function

Why does

struct MyStruct {
   auto foo () { return 1; }
   auto bar () { return foo(); }
};

compile, but when using a trailing return type like so:

struct MyStruct {
   auto foo () { return 1; }
   auto bar () -> decltype(foo()) { return foo(); }
};

compilation fails with

error: function 'foo' with deduced return type cannot be used before it is defined

Is this correct behavior on the implementations' part?

like image 846
zatm8 Avatar asked Dec 26 '16 09:12

zatm8


1 Answers

In the first snippet, we can deduce the (effective) return type, because the definition is provided at that lexical point—and conversely will not work if the definitions are lexically swapped, which is in unison with [dcl.spec.auto]/10, since we must disallow cyclic deduction.

Concerning the second snippet, see core issue 945, which effectively reopened core issue 643 and deals with this being used in trailing return types, in which the class type is incomplete still. AFAICS, current wording permits it in the same manner as in the first case (again given proper order of the definitions), but keep the open issue 1890 in mind; vendors defer implementation of questionable stuff until confirmed.

like image 66
Columbo Avatar answered Nov 19 '22 23:11

Columbo