185

This answer of @R. Martinho Fernandes shows, that the safe-bool idiom is apperently deprecated in C++11, as it can be replaced by a simple

explicit operator bool() const;

according to the standard quote in the answer §4 [conv] p3:

An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (§8.5). Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (§8.5).

The highlighted part clearly shows the "implicit explicit cast" (called "contextual conversion" in the standard) as @R. Martinho put it.

The "certain language constructs" that require that "implicit explicit cast" seem to be the following:

  • if, while, for (§6.4 [stmt.select] p4)
  • binary logical operators && and || (§5.14 [expr.log.and/or] p1 for both)
  • the logical negation operator ! (§5.3.1 [expr.unary.op] p9)
  • conditional operator ?: (§5.14 [expr.cond] p1)
  • static_assert (§7 [dcl.dcl] p4)
  • noexcept (§15.4 [except.spec] p2)

Is our assumption in the title correct? I hope we didn't overlook any potential drawbacks.

3
  • 31
    +1: I love this type of question that teaches me new things about the upcoming standard. Jun 25, 2011 at 18:57
  • 1
    You know what implicit explicit cast is missing in the standard... returning something from another operator bool. For example, if I have a shared_ptr member called p and have this method: operator bool() const { return p; }, it fails to compile. That's stupid IMO.
    – David
    Mar 2, 2016 at 18:56
  • What do you mean by "implicit explicit" cast, @David?
    – Sz.
    Aug 2, 2019 at 21:43

2 Answers 2

135

Yes. This is the example for problems with only having implicit user-defined conversions and explicit user-defined conversion operators were practically invented because of this problem and to replace all the safe-bool stuff with something a lot cleaner and more logical.

0
-6

I wouldn't call it "obsolete". Not everyone is taking the leap to C++11 (not even 1 year old) as of yet. And even if the a good amount of coders were, the ability to keep the code backwards compatible would be a must, considering this kind of idiom seems more sensible for libraries than for programs proper.

7
  • 35
    I was purely talking in the presence of C++11. This question neither touches old code, backwards compatability, or the unwillingness to change to C++11 aware compilers. Also note that C++11 in itself is not fully backwards compatable, it introduced breaking changes.
    – Xeo
    Dec 18, 2011 at 19:43
  • 4
    Wouldn't have been able to know that, sorry. I didn't consider only the answer linked at the beginning, but also the fact that the question is tagged [c++] and [c++-faq], that led me to think that evaluation of both stages of the language was relevant. Dec 22, 2011 at 22:07
  • 1
    You're certainly right though, I didn't explicitly state it in the question. I'll edit that in, thanks for the heads up.
    – Xeo
    Dec 22, 2011 at 22:59
  • 1
    This answer could really use updating, now that it's nearly two years old.
    – Puppy
    Oct 24, 2013 at 18:35
  • 1
    I'm gonna have to downvote due to disagreement, though I'd buy you a beer in person and say "hey no hard feelings". But many paradigms in C++11 were experiencing deployment as the --std=c++0x long before the final nail was driven into the standards coffin and they decided to put the name on the ISO spec. Unless you're a really deep template metaprogramming junkie, the details of the C++11 spec vs what people were using are likely of no consequence to you...which means it was older than 2011 for almost all practical purposes even then. And now, by my clock, it's nearly 2015. Dec 6, 2014 at 3:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.