struct A
{
// error C2216: 'explicit' cannot be used with 'virtual'
virtual explicit operator bool() const
{
return true;
}
};
struct B : A
{
// error C2216: 'explicit' cannot be used with 'override'
explicit operator bool() const override
{
return false;
}
};
int main()
{
if (A())
{}
if (B())
{}
}
我的编译器是 VC++ 2013 RC。
为什么explicit
与virtual
不兼容?
原因是什么?
最佳答案
看起来像一个错误,因为以下引用证明它们确实兼容,我找不到任何禁止它的内容。
12.3.2 转换函数[class.conv.fct]
2) A conversion function may be
explicit
[...]
[...]
5) Conversion functions can bevirtual
.
和
7.1.2 函数说明符[dcl.fct.spec]
5) The
virtual
specifier shall be used only in the initial declaration of a non-static class member function; see 10.3.
6) Theexplicit
specifier shall be used only in the declaration of a constructor or conversion function within its class definition; see 12.3.1 and 12.3.2.
关于c++ - 为什么 `explicit` 与 `virtual` 不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356232/