Is the safe-bool idiom obsolete in C++11?
Asked Answered
M

2

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.

Maribeth answered 5/6, 2011 at 12:1 Comment(3)
+1: I love this type of question that teaches me new things about the upcoming standard.Cythiacyto
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.Eastlake
What do you mean by "implicit explicit" cast, @David?Douglass
G
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.

Genesisgenet answered 5/6, 2011 at 12:3 Comment(0)
R
-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.

Reposition answered 18/12, 2011 at 19:32 Comment(7)
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.Maribeth
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.Reposition
You're certainly right though, I didn't explicitly state it in the question. I'll edit that in, thanks for the heads up.Maribeth
This answer could really use updating, now that it's nearly two years old.Genesisgenet
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.Ipoh
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.Reposition
Why would you compile with the default compiler for the default distro for production? Install a better one for your dev machines.Genesisgenet

© 2022 - 2024 — McMap. All rights reserved.