Boost logo

Boost :

Subject: Re: [boost] PR: Remove safe_bool idiom from boost.tribool
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2018-05-22 18:00:19


On 05/22/18 18:30, Robert Ramey via Boost wrote:
>
>>> Finally.  I believe that these are compile time errors in C++17.
>>
>> No, they are not.
>>
>> struct tribool
>> {
>>      int value;
>>
>>      EXPLICIT operator bool() const { return value != 0; }
>> };
>>
>> int main()
>> {
>>      tribool b1, b2;
>>      b1 < b2;
>>      b1 + b2;
>>      b1 + 5;
>>      b1 * 2;
>> }
>>
>> $ g++ -std=c++17 -DEXPLICIT= -o tribool_conv tribool_conv.cpp
>>
>> $ g++ -std=c++17 -DEXPLICIT=explicit -o tribool_conv tribool_conv.cpp
>> tribool_conv.cpp: In function ‘int main()’:
>> tribool_conv.cpp:11:8: error: no match for ‘operator<’ (operand types
>> are ‘tribool’ and ‘tribool’)
>>       b1 < b2;
>>       ~~~^~~~
>> tribool_conv.cpp:11:8: note: candidate: operator<(int, int) <built-in>
>> tribool_conv.cpp:11:8: note:   no known conversion for argument 2 from
>> ‘tribool’ to ‘int’
>> tribool_conv.cpp:12:8: error: no match for ‘operator+’ (operand types
>> are ‘tribool’ and ‘tribool’)
>>       b1 + b2;
>>       ~~~^~~~
>> tribool_conv.cpp:12:8: note: candidate: operator+(int, int) <built-in>
>> tribool_conv.cpp:12:8: note:   no known conversion for argument 2 from
>> ‘tribool’ to ‘int’
>> tribool_conv.cpp:13:8: error: no match for ‘operator+’ (operand types
>> are ‘tribool’ and ‘int’)
>>       b1 + 5;
>>       ~~~^~~
>> tribool_conv.cpp:13:8: note: candidate: operator+(int, int) <built-in>
>> tribool_conv.cpp:13:8: note:   no known conversion for argument 1 from
>> ‘tribool’ to ‘int’
>> tribool_conv.cpp:14:8: error: no match for ‘operator*’ (operand types
>> are ‘tribool’ and ‘int’)
>>       b1 * 2;
>>       ~~~^~~
>> tribool_conv.cpp:14:8: note: candidate: operator*(int, int) <built-in>
>> tribool_conv.cpp:14:8: note:   no known conversion for argument 1 from
>> ‘tribool’ to ‘int’
>
> Damn, you got me here. Good illustration. So the legal operations of a
> bool depends on whether it's the result of an explicit or implicit
> conversion.
I think you missed the point. The explicit mark prevents the (implicit)
conversion - it's not even invoked, so in the latter case bool is not
even involved. The compiler tries to find the operators that take
tribool verbatim or something that could be implicitly constructed from
it. And since tribool does not define the requested operators you get
the above errors.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk