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.

2 Answers2

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 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. –  Feb 21 '12 at 19:00
  • 1
    @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. – Nawaz Feb 21 '12 at 19:06
  • 1
    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. –  Feb 21 '12 at 19:11
  • @Fanael: I added something to answer. – Nawaz Feb 21 '12 at 19:24
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 */
}
  • 4
    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. –  Feb 21 '12 at 19:00