You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

显式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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:22:07