For now,
operator void() is allowed but never used (unless explicitly called with
obj.operator void() ).
I propose to add a meaning to this conversion operator: it is called when ever the result of an expression isn't used.
First, this is not the same as calling the destructor on a temporary object.
This would allow to call a specific function with that kind of syntax:
Basically, if the result of an expression is not used, it calls the (
implicit)
operator void().
If you write
(void) myobj or
static_cast<void>(myobj), it calls the explicit
operator void().
If you write
reinterpret_cast<void>(myobj), nothing happens:
no operator void() is called.
If the
operator void() should be called but doesn't exist, nothing happens:
no operator void() is called.
If the
operator void() should be called but is
deleted: the program is ill-formed.
If a function parameter is not used within the function body, nothing happens:
no operator void() is called.
I'm not sure what kind of uses this can have, but this would add a meaning to something that is already syntactically valid (you already can write
operator void() ), and some languages allow to call a function without parenthesis.
I'm pretty sure some people already have some use cases for this kind of features like static analysis of the code.
The pitfall I can see with this are the following:
- With the previous rules, myobj = expr; would call the operator void() because the operator=() returns a reference to the object which is not used.
A solution to this could be to not return a reference to the object after an affectation (this breaks the affectation chaining) or return a wrapper to the reference (add boilerplate).