|
Boost : |
Subject: Re: [boost] PR: Remove safe_bool idiom from boost.tribool
From: Robert Ramey (ramey_at_[hidden])
Date: 2018-05-22 15:30:42
>> 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. Such a think would never have occurred to me. I suppose
it's obvious what I think about that idea so I won't comment.
Another confession: I didn't realize that optional used and explicit
conversion. Maybe that changes something. I've never thought that
conversion to bool from optional was ever a good idea - be it implicit
or explicit. But I guess that's just me.
Robert Ramey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk