Reputation: 50548
According to the [dcl.constexpr/3]:
The definition of a constexpr function shall satisfy the following requirements:
[...]
- its function-body shall be = delete, = default, or [...]
This means that the following class snippet is valid:
struct S {
constexpr void f() = delete;
};
What's the purpose of having a deleted constexpr
function?
What are the benefit of defining it constexpr
if any?
I can't figure out any reason, but the fact that maybe it's easier to allow it than to forbid it in the standard.
Upvotes: 2
Views: 409
Reputation: 303537
This was based on CWG 1199. Daniel Krügler wrote:
it could be useful to allow this form in a case where a single piece of code is used in multiple configurations, in some of which the function is
constexpr
and others deleted; having to update all declarations of the function to remove the constexpr specifier is unnecessarily onerous.
Upvotes: 3
Reputation: 4275
I guess the purpose is the same as with any =delete
:
If you inherit from a class, but don't want the function to be available in subclasses.
E.g.:
class P{
public:
constexpr int foo(){return 42;}
};
class C : public P{
public:
constexpr int foo() = delete; //(*)
};
int main() {
P p;
cout << p.foo(); //ok
C c;
cout << c.foo(); //Compiler error only if line (*) is present.
return 0;
}
Even though i can't tell you right now where it is usefull - but i also can't see any reason right now why it should be forbidden.
Upvotes: 1