Incompatibilities between safe bool idiom and explicit operator bool
Asked Answered
T

2

7

I'm thinking of replacing all the instances of safe bool idiom by explicit operator bool in code which already uses C++11 features (so the fact that older compilers don't recognized explicit conversion operators will not matter), so I'd like to know if it can cause some subtle problems.

Thus, what are all the possible incompatibilities (even the most minute ones) that can be caused by switching from old and dull safe bool idiom to new and shiny explicit operator bool?

EDIT: I know that switching is a good idea anyway, for the latter is a language feature, well-understood by the compiler, so it'll work no worse than what's in fact just a hack. I simply want to know the possible differences.

Telson answered 21/2, 2012 at 18:45 Comment(0)
S
4

If you've used safe-bool conversion incorrectly in your code, only then explicit operator bool be incompatible, as it wouldn't allow you to do things incorrectly that easily. Otherwise, it should be just fine without any problem. In fact, even if there is problem, you should still switch to explicit operator bool, because if you do so, then you could identify the problem in the usage of the safe-bool conversion.

According to this article, some compilers emit inefficient instructions for safe-bool implementation using member function pointer,

When people started using this idiom, it was discovered that there was an efficiency penalty on some compilers — the member function pointer caused a compiler headache resulting in slower execution when the address was fetched. Although the difference is marginal, the current practice is typically to use a member data pointer instead of a member function pointer.

Skelly answered 21/2, 2012 at 18:52 Comment(4)
Of course you're right. But there's a reason I tagged this with language-lawyer. I'd like pure facts that follow from the standard itself, not an advice on good practices. Gotta clarify that, but thanks anyway.Telson
@Fanael: The Standards, C++03 and C++11 both, do not talk about safe-bool idiom, so it is not possible to quote from it to support what I said. All I'm implied is that C++11 has introduced explicit operator bool for reason(s), and one of the reasons, I think, is explicit operator bool is safer than the so-called safe-bool idiom.Skelly
But the standard talks about the things used to implement the safe-bool idiom. So, while the standard says naught about that idiom itself, its exact guarantees are pretty much implied by the document.Telson
@Fanael: I added something to answer.Skelly
S
4

Probably the biggest difference, assuming your code is free of bugs (I know, not a safe assumption), will be that in some cases, you may want an implicit conversion to exactly bool. An explicit conversion function will not match.

struct S1
{
    operator S1*() { return 0; } /* I know, not the best possible type */
} s1;

struct S2
{
    explicit operator bool() { return false; }
} s2;

void f()
{
    bool b1 = s1; /* okay */
    bool b2 = s2; /* not okay */
}
Sibel answered 21/2, 2012 at 18:56 Comment(1)
I had already noted that the pointer type I used isn't the best possible type, it serves fine as an example because the differences between this and a correct implementation are irrelevant to my answer, and this is more readable.Sibel

© 2022 - 2024 — McMap. All rights reserved.