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 08:55:27


On 05/22/18 07:47, Robert Ramey via Boost wrote:
> On 5/21/18 6:02 PM, Peter Dimov via Boost wrote:
>> Robert Ramey wrote:
>>
>>> > I'm strongly opposed to downgrading from safe_bool to implicit bool.
>>>
>>> I made my argument why this is good idea.  What's wrong with my
>>> argument?
>>
>> Defining a conversion to bool enables unwanted operations such as
>>
>>     tribool b1, b2;
>>
>>     b1 < b2;
>>     b1 + b2;
>>     b1 + 5;
>>     b1 * 2;
>>
>> That's the whole point of "safe bool", to avoid those.
>
> Right.
>
> But - Since the have been prohibited by the current safe_bool, there are
> none in current code. So the change won't break anything.

It won't break the current code, but it will open avenue for bugs like
that in the future code.

> 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’


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