safe-bool-idiom

How does the safe bool idiom bool_type (and the safe bool idiom) work?

☆樱花仙子☆ 提交于 2021-02-17 06:04:47
问题 I was pointed to the 'safe bool idiom', and after trying to decipher what is going on (the explanation supplied on the site was not sufficient enough to grant me understanding of why it works), I decided to try to take the following code apart and make an attempt at simplifying it as much as possible. The site supplied code below: class Testable { bool ok_; typedef void (Testable::*bool_type)() const; void this_type_does_not_support_comparisons() const {} public: explicit Testable(bool b=true

How does the safe bool idiom bool_type (and the safe bool idiom) work?

时光怂恿深爱的人放手 提交于 2021-02-17 06:03:50
问题 I was pointed to the 'safe bool idiom', and after trying to decipher what is going on (the explanation supplied on the site was not sufficient enough to grant me understanding of why it works), I decided to try to take the following code apart and make an attempt at simplifying it as much as possible. The site supplied code below: class Testable { bool ok_; typedef void (Testable::*bool_type)() const; void this_type_does_not_support_comparisons() const {} public: explicit Testable(bool b=true

Was boost::bool_testable<> relocated or removed?

一个人想着一个人 提交于 2019-12-30 11:07:22
问题 I'm trying to leverage boost::bool_testable<> (from Boost.Operators) to implement the safe bool idiom for a class, but the most recent version of the library (1.49 as of this post) doesn't seem to have it anymore. Where did it go? Is there a better alternative available now, and I've just missed it? I'm not able to use C++11 and therefore cannot use the language extensions that render safe bool unnecessary. 回答1: It's a late answer, but I'm only active on Stack Overflow for a short time and I

Was boost::bool_testable<> relocated or removed?

若如初见. 提交于 2019-12-30 11:07:03
问题 I'm trying to leverage boost::bool_testable<> (from Boost.Operators) to implement the safe bool idiom for a class, but the most recent version of the library (1.49 as of this post) doesn't seem to have it anymore. Where did it go? Is there a better alternative available now, and I've just missed it? I'm not able to use C++11 and therefore cannot use the language extensions that render safe bool unnecessary. 回答1: It's a late answer, but I'm only active on Stack Overflow for a short time and I

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

假如想象 提交于 2019-12-27 10:21:33
问题 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

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

扶醉桌前 提交于 2019-12-27 10:20:08
问题 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

Is there a safe bool idiom helper in boost? [closed]

半城伤御伤魂 提交于 2019-12-10 00:51:58
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . 25% of programmers work time is spended by checking if the required code already exist. I'm searching for a base class for implementing the safe bool idiom. 回答1: bool_testable<> in Boost.Operators looks promising

Incompatibilities between safe bool idiom and explicit operator bool

 ̄綄美尐妖づ 提交于 2019-12-08 18:24:35
问题 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

Is there a safe bool idiom helper in boost? [closed]

…衆ロ難τιáo~ 提交于 2019-12-04 22:56:44
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . Closed 8 years ago . 25% of programmers work time is spended by checking if the required code already exist. I'm searching for a base class for implementing the safe bool idiom. bool_testable<> in Boost.Operators looks promising. The reference mentions that: bool_testable provides the antithesis of operator bool , such that the

Are there cases where a typedef is absolutely necessary?

老子叫甜甜 提交于 2019-12-03 10:30:12
问题 Consider the following excerpt from the safe bool idiom: typedef void (Testable::*bool_type)() const; operator bool_type() const; Is it possible to declare the conversion function without the typedef? The following does not compile: operator (void (Testable::*)() const)() const; 回答1: Ah, I just remembered the identity meta-function. It is possible to write operator typename identity<void (Testable::*)() const>::type() const; with the following definition of identity : template <typename T>