Question: Is this function pointer with `this` in trailing return type legal?

Question

Is this function pointer with `this` in trailing return type legal?

Answers 1
Added at 2017-11-07 19:11
Tags
Question
class C {
  auto (*foo)() -> decltype(this);
};

This code is accepted by GCC, MSVC, and clang, but not icc.

Answers to

Is this function pointer with `this` in trailing return type legal?

nr: #1 dodano: 2017-11-07 20:11

Quoting n4140 (roughly C++14) [expr.prim.general]:

3 If a declaration declares a member function or member function template of a class X, the expression this is a prvalue of type "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq and the end of the function-definition, member-declarator, or declarator. It shall not appear before the optional cv-qualifier-seq and it shall not appear within the declaration of a static member function (although its type and value category are defined within a static member function as they are within a non-static member function). [...]

4 Otherwise, if a member-declarator declares a non-static data member (9.2) of a class X, the expression this is a prvalue of type "pointer to X" within the optional brace-or-equal-initializer. It shall not appear elsewhere in the member-declarator.

Since you're not declaring a member function or member function template, p3 doesn't apply, but this is what would make the code valid for the non-pointer case where you actually declare a member function: the trailing return type is between the optional cv-qualifier-seq and the end of the declarator, as made clearer in the definition of a const member function:

auto foo() const -> decltype(this) { }

p4 is what applies here though. It allows this only to appear in the initialiser. You're putting it elsewhere. p3 doesn't apply, so ICC is correct to reject this.

Source Show
◀ Wstecz