|
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