Question
Explicit this object parameter wonkiness
There was some code floating around on Reddit that defined a member-function with an explicit this
object parameter defined as type int
. This made me wonder how this member-function could possibly be invoked.
After doing some tests, all of the compilers (Clang, GCC, and MSVC) seem to produce different results. So, this raises the question; is the following code legal and in accordance with the C++23 standard?
struct w { constexpr bool f(this int) { return true; } };
static_assert((*&w::f)(1)); // clang ok, gcc ok, msvc nope
static_assert((*&w::f)(0)); // clang ok, gcc nope, msvc nope
GCC's error message:
<source>:3:23: error: non-constant condition for static assertion
3 | static_assert((*&w::f)(0));
| ~~~~~~~~^~~
<source>:3:24: error: dereferencing a null pointer
3 | static_assert((*&w::f)(0));
|
MSVC's error message:
<source>(2): error C2660: 'w::f': function does not take 1 arguments
<source>(1): note: see declaration of 'w::f'
<source>(2): note: while trying to match the argument list '(int)'
<source>(3): error C2660: 'w::f': function does not take 1 arguments
<source>(1): note: see declaration of 'w::f'
<source>(3): note: while trying to match the argument list '(int)'