I have this code and GCC prints "what!?". How can I avoid that, so that the void cast simply has the C meaning "Ignore the lonely 'a;'"?
#include <iostream>
struct A {
template<typename T>
operator T() {
std::cout << "what!?";
}
};
int main() {
A a;
(void)a;
}
As you've observed, this is a bug in gcc. The standard reads:
c++11
12.3.2 Conversion functions [class.conv.fct]
(1) A conversion function is never used to convert a (possibly cv-qualified) object to [...] (possibly cv-qualified) void.
116) A conversion to void does not invoke any conversion function (5.2.9).5.2.9 Static cast [expr.static.cast]
(6) Any expression can be explicitly converted to type cv void, in which case it becomes a discarded-value expression (Clause 5).
As a workaround, you could write:
a, void();
It's impossible to overload operator,(void)
so there is zero chance of this invoking user-defined behaviour from a conformant implementation.
Adding an
operator void() {}
takes care of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With