On 12.04.2019 18:40, Ben Bacarisse wrote:
> "Alf P. Steinbach" <
alf.p.stein...@gmail.com> writes:
>
>> On 12.04.2019 09:41, Juha Nieminen wrote:
> <cut>
>>> Pointers implicitly casting to bool is inherited from C. Whether
>>> that's a good or bad thing is up to opinion.
>>
>> But here I disagree. Technically it's up to one's opinion. But I fail
>> to see any advantage of the implicit conversion to `bool` in modern
>> programming.
>>
>> An explicit cast like `!!p` (which used to also help with inane
>> sillywarnings about performance from the Visual C++ compiler) is more
>> clear and not significantly more to write, and avoids all the implicit
>> conversion problems that Andy's code is one example of.
>
> It doesn't avoid it -- it relies on it.
In the language that we have, yes.
> Both the initialisation of a
> bool from a pointer, and the application of ! to a pointer, use the
> standard conversions,
Yes, but that's pretty irrelevant. Nobody's contested that or failed to
understand it. Assuming that Andy posted his question more as a possibly
interesting teaser that he fully knew the answer to, than as a way to
learn what was going on.
> which is what you and Juha (and almost everyone!)
> calls implicit conversions.
“Implicit conversion” is a description, a descriptive term; there is no
need to be pedantic about it. In fact the formal term “standard
conversion” is more limited, namely an implicit conversion with built-in
meaning (see C++17 §7/1). As I understand it formal “standard
conversion” does therefore /not/ cover use of a non-`explicit`
conversion operator, which is a case that both formal and informal
“implicit conversion” do cover.
Summing up, pedantry has its costs, so use with care. ;-)
> Of course C++ could be changed to remove this standard pointer to bool
> conversion and to add a special rule about pointer operands of !, but
> that's not how things stand at the moment.
Yes.
Which is why I suggested a `Strict_bool` class in the immediately
following text, a clarifying supportive context and what the earlier
remarks led up to, that you snipped.
The current version of that class, called `Truth`, is available at <url:
https://github.com/alf-p-steinbach/cppx-core/blob/master/source/cppx-core/language/types/Truth.hpp>.
It's mostly a drop-in replacement for `bool`, avoiding the undesired
implicit conversions.
As of C++17 there is one usage where `bool` can't be replaced with
`Truth` or a similar class, namely as a template value parameter. As I
understand it that situation will change with C++20. However, template
value parameters only introduce limited scope for conversion problems.
Cheers & hth.,
- Alf