此GCC 12.1常量是错误还是功能? “尝试使用const对象调用非const函数”

发布于 2025-01-29 15:55:04 字数 1703 浏览 3 评论 0原文

我们看到的C ++代码在GCC 11.3和Visual Studio 2022中成功编译,并在GCC 12.1上存在问题。该代码在 Compiler Explorer https://godbolt.org/z/6pyecsd1h (感谢@nathanpierson对此进行简化

。即使可以使用const超载,也可以在const函数中进行基类功能。这似乎是某种编译器错误,但这可能是我不理解的一些新的C ++规则。这代表编译器错误吗?

struct BaseClass
{
    // Commenting this non-const function out will also fix the compilation.
    int* baseDevice() { return nullptr; }
    const int* baseDevice() const { return nullptr; }
};

template <class ObjectClass>
struct DerivedClass : BaseClass
{

};

template <class ObjectClass>
struct TopClass : DerivedClass<ObjectClass>
{
  public:
    virtual int failsToCompile() const
    {
      // This should choose to call the const function, but it tries to call the non-const version.
      if (BaseClass::baseDevice())
         return 4;

      return 1;
    }
};

int main()
{
    TopClass<int> x;
}
<source>: In instantiation of 'int TopClass<ObjectClass>::failsToCompile() const [with ObjectClass = ConcreteObject]':
<source>:27:17:   required from here
<source>:30:32: error: passing 'const TopClass<ConcreteObject>' as 'this' argument discards qualifiers [-fpermissive]
   30 |       if (BaseClass::baseDevice())
      |           ~~~~~~~~~~~~~~~~~~~~~^~
<source>:14:15: note:   in call to 'MyDevice* BaseClass::baseDevice()'
   14 |     MyDevice* baseDevice() { return nullptr; }
      |               ^~~~~~~~~~
ASM generation compiler returned: 1

We're seeing C++ code, that compiles successfully in GCC 11.3 and Visual Studio 2022, have issues with GCC 12.1. The code is on Compiler Explorer: https://godbolt.org/z/6PYEcsd1h (Thanks to @NathanPierson for simplifying it some.)

Basically, a template class is deciding to try to call a non-const base class function in a const function, even though a const overload is available. This appears to be some sort of compiler bug, but it could be some weird new C++ rule I don't understand. Does this represent a compiler bug?

struct BaseClass
{
    // Commenting this non-const function out will also fix the compilation.
    int* baseDevice() { return nullptr; }
    const int* baseDevice() const { return nullptr; }
};

template <class ObjectClass>
struct DerivedClass : BaseClass
{

};

template <class ObjectClass>
struct TopClass : DerivedClass<ObjectClass>
{
  public:
    virtual int failsToCompile() const
    {
      // This should choose to call the const function, but it tries to call the non-const version.
      if (BaseClass::baseDevice())
         return 4;

      return 1;
    }
};

int main()
{
    TopClass<int> x;
}
<source>: In instantiation of 'int TopClass<ObjectClass>::failsToCompile() const [with ObjectClass = ConcreteObject]':
<source>:27:17:   required from here
<source>:30:32: error: passing 'const TopClass<ConcreteObject>' as 'this' argument discards qualifiers [-fpermissive]
   30 |       if (BaseClass::baseDevice())
      |           ~~~~~~~~~~~~~~~~~~~~~^~
<source>:14:15: note:   in call to 'MyDevice* BaseClass::baseDevice()'
   14 |     MyDevice* baseDevice() { return nullptr; }
      |               ^~~~~~~~~~
ASM generation compiler returned: 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2025-02-05 15:55:04

是这个GCC 12.1常量问题一个错误或功能

这是一个错误。我提交了a bug Report a href =“ https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git; h = 2decd2cabe5a4f“ rel =“ noreferrer”>此提交

该票已经被分配了,该分辨率具有第12.2版的针对性里程碑 - 因此我们希望快速修复。

Is this gcc 12.1 const problem a bug or feature

It's a bug. I filed a bug report and the issue has already been verified coming from this commit.

The ticket has been assigned and the resolution has a targeted milestone of version 12.2 - so we can hope for a quick fix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文