这个 GCC 12.1 const 问题是错误还是功能? "Attempts to call non-const function with const object"

Is this GCC 12.1 const problem a bug or feature? "Attempts to call non-const function with const object"

我们看到在 GCC 11.3 和 Visual Studio 2022 中成功编译的 C++ 代码在 GCC 12.1 中存在问题。代码在 Compiler Explorer: https://godbolt.org/z/6PYEcsd1h(感谢@NathanPierson 对其进行了一些简化。)

基本上,模板 class 决定尝试在 const 函数中调用非常量基础 class 函数,即使 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

Is this gcc 12.1 const problem a bug or feature

这是一个错误。我提交了 bug report and the issue has already been verified coming from this commit.

工单已分配,解决方案的目标里程碑是版本 12.2 - 因此我们希望能快速修复。