显式this成员函数代码MSVC可编译,Clang/GCC报错,是否符合C++标准?
显式
this参数的C++代码是否符合标准? 我编写了一段使用显式this参数的C++成员函数代码,该代码可在MSVC中编译通过,但会被Clang和GCC拒绝。
代码如下:
#include <iostream> struct C{ int f(this int); }; decltype(C::f) func; // MSVC接受,但Clang和GCC拒绝 int func(int j) { std::cout << "called with: " << j << std::endl; return 5; }
GCC的报错信息如下:
<source>:7:13: error: invalid use of non-static member function 'int C::f(this int)' 7 | decltype(C::f) func; | ^ <source>:7:13: error: invalid use of non-static member function 'int C::f(this int)' <source>:7:13: error: invalid use of non-static member function 'int C::f(this int)' | <source>: At global scope: <source>:13:15: error: 'int func(int)' redeclared as different kind of entity 13 | int func(int j) | ^ <source>:7:16: note: previous declaration 'int func' 7 | decltype(C::f) func; | ^~~~
我想咨询:这段程序是否符合C++标准,属于良构还是非良构代码?
内容的提问来源于stack exchange,提问作者user20562802
相关产品推荐
相关产品推荐

