Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explicit operator bool error

I get the Compiler Error C2071 when I try to implement the explicit operator bool:

class C
{
public:

    explicit operator bool() const
    {
        return !!*this;
    }
};

Why? How can I solve this problem?
I'm using Visual Studio 2012 RC.

like image 990
Nick Avatar asked Jul 06 '12 15:07

Nick


People also ask

What is an explicit operator?

Explicit conversion operators are those that do require an explicit cast. As an example, the following code shows a simple console application to covert a weight in Pounds to Kilograms.

What is bool operator in C++?

bool operator<( const TriIndex& rhs ) is the "less than" operating ( < ), bool is the return type of the "less than" operator.


2 Answers

Visual Studio 2012 does not support explicit conversion operators, see C++11 Features in Visual C++ 11.

These articles talk about the safe bool idiom:

  • http://www.artima.com/cppsource/safebool.html
  • http://en.wikibooks.org/wiki/More_C++_Idioms/Safe_bool
like image 124
gliderkite Avatar answered Oct 13 '22 05:10

gliderkite


If you look at a list of features in Visual Studio 2010 you can see that it was not an available feature. A look at What's New for Visual C++ in Visual Studio 2012 shows that is has not been added.

like image 42
NominSim Avatar answered Oct 13 '22 05:10

NominSim