Код: Выделить всё
class MyBool{
private:
bool value_ = false;
// Nice API and logic to set the interal value_ to true or false...
public:
explicit operator bool() const {
return value_;
};
};
[*]. Operators! , && и || ;
[*] Первый операнд условного оператора ?: ;
[*] Предикат в STATIC_ASSERT Declaration;
[*] выражение в noExctement
[*] выражение
[*] выражение
. /> < /ul>
, что объясняет, почему я получаю ошибки при попытке использовать его другими способами, например: < /p>
Код: Выделить всё
MyBool x;
///...
bool y = x; // --> error: cannot convert ‘MyBool’ to ‘bool’ in initialization
// or:
bool z;
z = x; // --> error: cannot convert ‘MyBool’ to ‘bool’ in assignment
// Or returning from a function:
bool func(...){
MyBool x;
// ...
return x; // --> error: cannot convert ‘MyBool’ to ‘bool’ in return
}
// Or in Gtests, like:
MyBool x;
// ...
EXPECT_TRUE(x); // --> error: no matching function for call to ‘testing::AssertionResult::AssertionResult(MyBool)’
Код: Выделить всё
bool get_value() const {
return value_;
}
Подробнее здесь: https://stackoverflow.com/questions/613 ... overloaded