这是一个例子:
struct TestClass {
void testFinction() {}
static TestClass* ptr_;
constexpr static auto funcPtr = +[](){ ptr_->testFinction(); };
};
TestClass* TestClass::ptr_ = new TestClass();
int main() {
TestClass::funcPtr();
delete TestClass::ptr_;
return 0;
}
这会在
gcc
中编译,但不会在 clang
和 msvc
中编译。哪个编译器是正确的?这是gcc
的bug吗?
这是
clang
错误消息:
<source>:4:46: error: member access into incomplete type 'TestClass'
4 | constexpr static auto funcPtr = +[](){ ptr_->testFinction(); };
| ^
<source>:1:8: note: definition of 'TestClass' is not complete until the closing '}'
1 | struct TestClass {
| ^
1 error generated.
Compiler returned: 1
msvc
错误信息如下:
example.cpp
<source>(4): error C2027: use of undefined type 'TestClass'
<source>(1): note: see declaration of 'TestClass'
<source>(4): error C2131: expression did not evaluate to a constant
<source>(4): note: failure was caused by call of undefined function or one not declared 'constexpr'
<source>(4): note: see usage of 'TestClass::<lambda_bdad2d00eade3d9eccb85c581305196c>::operator void (__cdecl *)(void)'
Compiler returned: 2