此GCC 12.1常量是错误还是功能? “尝试使用const对象调用非const函数”
我们看到的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误。我提交了a bug Report a href =“ https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git; h = 2decd2cabe5a4f“ rel =“ noreferrer”>此提交。
该票已经被分配了,该分辨率具有第12.2版的针对性里程碑 - 因此我们希望快速修复。
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.