[PATCH?] 'this' in typedefs, possibly overly complicated code in other 'this' checks?

Harald van Dijk harald at gigawatt.nl
Wed Jan 15 15:30:35 PST 2014


Hi all,

This came up on StackOverflow recently:

'this' is rejected by clang in static member functions:

error: 'this' cannot be used in a static member function declaration
  static auto f() -> decltype(this);
                              ^

However, what the standard actually says is that 'this' is not allowed
except in non-static member functions (and some more bits not relevant
here). Declarations that look like member functions but aren't, not even
static ones, shouldn't allow 'this' either.

  typedef auto f() -> decltype(this);

Looking to see what clang implements, I was very surprised. There is
already perfectly functional code that rejects 'this' in 'friend'
functions, and that code is not reused for 'static' functions (and
'typedef's): instead, the checks have effectively been rewritten for
'static'.

I'm wondering, why is that? If I do attempt to re-use the existing code,
as in the attached patch, then the only changes in the test results are
actually correct, as references to non-static data members are permitted
even in 'static' functions, so long as they appear in unevaluated
expressions. There is even a FIXME comment about this in the test. But
I'm sure the current checks have been added for a good reason. Am I
overlooking some important details that are not covered by the testsuite?

Cheers,
Harald van Dijk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Reject-this-in-typedefs-allow-implicit-this.patch
Type: text/x-patch
Size: 11927 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140116/c11c27b7/attachment.bin>


More information about the cfe-commits mailing list