safe-bool-idiom

Are there cases where a typedef is absolutely necessary?

隐身守侯 提交于 2019-12-03 02:03:50
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; 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> struct identity { typedef T type; }; You could argue that identity still uses a typedef , but this solution

Was boost::bool_testable<> relocated or removed?

我怕爱的太早我们不能终老 提交于 2019-12-01 11:08:54
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. It's a late answer, but I'm only active on Stack Overflow for a short time and I just found your question. I am the maintainer of Boost.Operators and I removed bool_testable back in

Safe bool idiom in boost?

旧城冷巷雨未停 提交于 2019-11-29 06:05:42
Does the boost library provide an implementation of a safe bool idiom, so that I could derive my class from it? If yes - where is it? If no - what are my alternatives beyond implementing it myself? I found the following similar question: " Is there a safe bool idiom helper in boost? " and the accepted answer suggests using bool_testable<> in Boost.Operators . Unfortunately, when I checked the boost manual I couldn't find it there. Code using it fails to compile too. I also stumbled on another SO question " Was boost::bool_testable<> relocated or removed? " and the comment there suggests that

Safe bool idiom in boost?

爷,独闯天下 提交于 2019-11-27 23:33:37
问题 Does the boost library provide an implementation of a safe bool idiom, so that I could derive my class from it? If yes - where is it? If no - what are my alternatives beyond implementing it myself? I found the following similar question: " Is there a safe bool idiom helper in boost? " and the accepted answer suggests using bool_testable<> in Boost.Operators. Unfortunately, when I checked the boost manual I couldn't find it there. Code using it fails to compile too. I also stumbled on another

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

北城以北 提交于 2019-11-26 06:00:00
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