Consider the following code:
#include <iostream>
class first
{
public:
constexpr first(bool val) noexcept : _value{val} {}
constexpr operator bool() const noexcept {return _value;}
private:
bool _value;
};
class second
{
public:
constexpr second(first val) noexcept : _value{val} {}
constexpr operator first() const noexcept {return _value;}
private:
first _value;
};
int main(int argc, char** argv)
{
first f{false};
second s{true};
bool b1 = f;
bool b2 = s; // Not compiling
return 0;
}
Until recently, I was thinking that the standard and compilers were "clever" enough to find the required conversion sequence when it exists.
In other words, I was thinking that bool b2 = s
would convert s
to first
, and then to bool
. But apparently it is not happening.
What is the correct approach to obtain an equivalent behaviour for first
and second
?