explicit operator bool error

坚强是说给别人听的谎言 提交于 2019-12-01 00:01:00

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/11365129/explicit-operator-bool-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!