Reputation: 530
Virtual functions cannot be constexpr
however, when a function is implicitly virtual through inheritance, the compilers I have tried don't complain about it.
Here is a sample code:
class A
{
virtual void doSomething() {}
};
class B : public A
{
constexpr void doSomething() override {} // implicitly virtual constexpr
// but no compilation error
};
class C : public A
{
virtual constexpr void doSomething() override {} // explicitly virtual constexpr
// compilation error
};
I tried it with gcc 7.2.0
and .clang 5.0.0
Are those compilers not compliant to the standard in this regard, or are implicitly virtual constexpr
functions allowed ?
Upvotes: 10
Views: 255
Reputation: 48958
The compilers have a bug. Note that this has been fixed in clang 3.5 already, not sure why you don't get an error, because I do.
The standard is pretty explicit about this in [dcl.constexpr]p3:
The definition of a constexpr function shall satisfy the following requirements:
- it shall not be virtual;
- [...]
It does't matter whether doSomething
is implicitly virtual
or not. In both cases, it is considered to be virtual
, and so it violates the point above.
Upvotes: 8