I'm subclassing std::optional
and need to delete the operator==(bool)
and implement my custom operator==(enum)
.
To delete the operator, this worked:
constexpr bool operator == ( bool ) noexcept = delete;
Works great for the code below, throwing a "deleted function" compile error
OptionalSubclass<int> ReturnEvens( int i ) { if ( i % 2 == 0 ) return i; return {}; }
:
:
auto result = ReturnEvens(42);
if ( result == true )
std::cout << *result << " is even" << std::endl;
However, the below code, with an implied 'true', compiles and executes
auto result = ReturnEvens(42);
if ( result )
std::cout << *result << " is even" << std::endl;
Is there a different operator I should be deleting?
std::optional
has an operator bool()
that allows it to be converted to a bool
for evaluation in a condition. You need to delete that operator as well so it cannot be implicitly converted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.