Boost Optional implicit conversion to bool?

Ventu

I'm reading some code and I came across something I do not understand. Its about testing if a Boost::optional value is initialised or not. It uses the gtest framework which provides the ASSERT_TRUE() macro.

#include "gtest\gtest.h"

void test() {
    boost::optional<someClass> opt = someFunc();
    ASSERT_TRUE(!!opt);
}

Why do I need the !! before opt? Is a boost::optional not implicitly converted to a bool, which is needed by the macro? I thought it would be enough to use ASSERT_TRUE(opt) to check if opt holds a correct value?

user743382

Is a boost::optional not impicit converted to a bool

No, it's not. Its conversion operator to bool is marked explicit, but your testing framework needs something that's implicitly convertible. You should see the problem with plain bool test = opt; too: that should fail to compile.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Did boost::optional's implicit cast to bool go away?

From Dev

boost::optional and type conversion

From Dev

Implicit conversion of return expressions to bool

From Dev

C++ Implicit conversion from bool to string

From Dev

Implicit conversion to bool for std::exeption_ptr

From Dev

boost::optional to bool, inside boost::spirit::qi grammar

From Dev

Why implicit conversion of bool to string isn't an error?

From Dev

C++, does bool conversion always fall back to implicit conversion to void*?

From Dev

Why isn't std::optional<boost::variant<A, bool>> successfully constructed from `std::optional<A>`?

From Dev

C++11 Lambda functions implicit conversion to bool vs. std::function

From Dev

C++11 Lambda functions implicit conversion to bool vs. std::function

From Dev

Is it called "implicit casting" or "implicit conversion"?

From Dev

Scala - Implicit conversion to implicit argument

From Dev

Scala implicit conversion of implicit argument

From Dev

The implicit unwrapping of an optional boolean

From Dev

Implicit conversion to lvalue reference

From Dev

Implicit conversion in the other direction

From Dev

No implicit conversion of Array into String?

From Dev

No implicit conversion of file into string

From Java

Implicit conversion and operator overload

From Dev

Second order implicit conversion

From Dev

Implicit conversion sequence

From Dev

Implicit conversion for generic type?

From Java

Implicit conversion not allowed on return

From Dev

Implicit conversion for multiple parameters

From Dev

Swift implicit conversion of type

From Dev

Implicit conversion with operator

From Dev

No warning on implicit conversion

From Dev

Implicit conversion of function type

Related Related

HotTag

Archive