180

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.

Community
  • 1
  • 1
Xeo
  • 123,374
  • 44
  • 277
  • 381
  • 30
    +1: I love this type of question that teaches me new things about the upcoming standard. – Björn Pollex Jun 25 '11 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 02 '16 at 18:56
  • What do you mean by "implicit explicit" cast, @David? – Sz. Aug 02 '19 at 21:43

2 Answers2

129

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.

Puppy
  • 138,897
  • 33
  • 232
  • 446
-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.

Luis Machuca
  • 942
  • 8
  • 14
  • 34
    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 '11 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. – Luis Machuca Dec 22 '11 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 '11 at 22:59
  • 1
    This answer could really use updating, now that it's nearly two years old. – Puppy Oct 24 '13 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. – HostileFork says dont trust SE Dec 06 '14 at 03:34
  • 1
    Am interested. What would need to be updated about the answer? While even C++11 is already being considered by some people as "old", C++03 seems to still be the default mode for the default compiler for the "default" distros I've seen in any production system. The safe-bool idiom, that didn't fail with C++03, is still not going to fail for them, and is not going to fail with the newer modes of operation either. The idiom itself would not be obsolete. At most, its expression [would be hidden](http://ryan.gulix.cl/fossil.cgi/cxxomfort/wiki?name=Features/explicit_cast). – Luis Machuca Aug 17 '15 at 04:22
  • Why would you compile with the default compiler for the default distro for *production*? Install a better one for your dev machines. – Puppy May 09 '17 at 18:06