Explicit this object parameter wonkiness

  Kiến thức lập trình

So, I was toying around with some little code snippet that I’d seen passing by on Reddit, and it made me wonder if the code is actually legal and in accordance with the C++23 standard. Because, as it seems, all of the major compilers (Clang, GCC, and MSVC), have something different to say about it.

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

Demo


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)'

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT