decltype deducted result of in-class defined function

361 views Asked by At

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?

1

There are 1 answers

11
Columbo On BEST ANSWER

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.