The following program
#include <type_traits>
struct S {
void f() const {
(void)[*this]() mutable {
static_assert(
std::is_same_v<decltype(this), S *>); // Fails with GCC & MSVC
static_assert(
std::is_same_v<decltype(this), const S *>); // Fails with Clang
(void)this;
}
();
}
};
int main() {}
contains a mutable lambda which captures *this by value ([*this]), and which does so in a const-qualified member function. The program is rejected by GCC due to #1, and rejected by Clang due to #2 (and conversely accepted):
- GCC(1) and MSVC thinks
decltype(this)isconst S * - Clang(1) thinks
decltype(this)isS *
(1) Various compiler versions for C++17 and C++20
DEMO.
Which compiler is right here?