Example (compiles fine)
struct A
{
void f() {};
auto g() -> decltype(f())
{}
};
Question
If I add the this pointer inside decltype (i.e. decltype(this->f())), I get the following compile errors with gcc 4.7.0:
error: invalid use of incomplete type 'struct A'
error: forward declaration of 'struct A'
error: invalid use of incomplete type 'struct A'
error: forward declaration of 'struct A'
Is using this in decltype not allowed? Could someone help me understand what is the problem?
EDIT
It seems the problem isn’t that
thisappears inside adecltype, but that it appears outside the function body.For example, this code below compiles under GCC 4.7:
This uses
decltype(this->f())inside the body ofg, but the specification of the return type of the function in theauto .... -> ....form, i.e. the so-called trailing return type specification, is not part of the function body, and GCC does not allow it there.However, it would appear (see discussion in comments) that the C++ Standard does not actually require
thisto be used in the function body: §5.1.1 of the standard states thatthismay be used anywhere between the optional const/volatile qualifier and the end of the function body, see clause 3 below. (For the sake of completeness, I have added clause 4 as well, which talks about data members and is not directly relevant to the question).NB: The optional cv-qualifier-seq, i.e. the
constorvolatilequalifier for the function must, as Jesse points out in the comment, appear before the trailing return type declaration. Hence usingthisin the way described in the question should be correct, and GCC seems to be wrong.