显式bool操作符 - 不能返回,测试,初始化bool [英] Explicit bool operator - cannot return, test, initialize bool

查看:518
本文介绍了显式bool操作符 - 不能返回,测试,初始化bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚尝试使用显式运算符bool()第一次,它的行为是非常意外的对我。有人可以告诉我们为什么下面标有 //不起作用的部分不起作用

I just tried to use explicit operator bool() for the first time and its behavior is quite unexpected to me. Can someone please shed some light on why the following sections marked with // does not work.

c $ c> convertible class would eg是一个智能指针类,能够检查所包含数据的有效性。

The convertible class would e.g. be a smart pointer class with the ability to check for the validity of the contained data.

struct convertible
{
  explicit operator bool() const
  {
    return ptr;
  }

  void* ptr = nullptr;
};

bool testReturn()
{
  convertible test;
  // does not work
  return test;
}

bool testReturn2()
{
  convertible test;
  // does not work
  return test == true;
}

bool testReturn3()
{
  convertible test;
  // works ?!
  return !test;
}

int main()
{
  convertible test;
  // works
  if (test) { }
  // works
  bool init(test);
  bool tested = test;
  bool tested2 = testReturn();
  bool tested3 = testReturn2();
  bool tested4 = testReturn3();
  return 0;
}

使用GCC我可以得到:

With GCC I get:

milian@minime:/tmp$ g++ --version
g++ (GCC) 4.8.2 20131219 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

milian@minime:/tmp$ g++ -std=c++11 test.cpp 
test.cpp: In function ‘bool testReturn()’:
test.cpp:15:10: error: cannot convert ‘convertible’ to ‘bool’ in return
  return test;
          ^
test.cpp: In function ‘bool testReturn2()’:
test.cpp:22:15: error: no match for ‘operator==’ (operand types are ‘convertible’ and ‘bool’)
  return test == true;
              ^
test.cpp:22:15: note: candidate is:
test.cpp:22:15: note: operator==(int, int) <built-in>
test.cpp:22:15: note:   no known conversion for argument 1 from ‘convertible’ to ‘int’
test.cpp: In function ‘int main()’:
test.cpp:39:17: error: cannot convert ‘convertible’ to ‘bool’ in initialization
  bool tested = test;

与Clang类似:

milian@minime:/tmp$ clang++ --version
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
milian@minime:/tmp$ clang++ -std=c++11 test.cpp 
test.cpp:15:10: error: no viable conversion from 'convertible' to 'bool'
  return test;
        ^~~~
test.cpp:22:15: error: invalid operands to binary expression ('convertible' and 'int')
  return test == true;
        ~~~~ ^  ~~~~
test.cpp:39:8: error: no viable conversion from 'convertible' to 'bool'
  bool tested = test;
      ^        ~~~~
3 errors generated.


推荐答案

如果您的目标是停用某些转化,他们通过 delete 说明符:

If your goal is to disable certain conversions, just disable them through the delete specifier:

struct convertible
{
    operator bool() const //(Implicit) Conversion to bool allowed
    {
        return true;
    }

    operator int() const = delete; //Implicit/Explicit conversions to int disallowed 
                                   //(Results in compilation error).
};


int main()
{
    convertible c;

    if( c )
        ...

    int a = c; //ERROR
    int b = (int)c; //ERROR (The same for static_cast<int>(c) )
}

也可以删除除了使用转换运算符的模板版本显式重载的所有类型:

You could also delete all types except which are explicitly overloaded using a templated version of the conversion operator:

struct foo
{
    operator bool() const //Explicit overload for bool
    {
       return true; 
    }

  template<typename T>
  operator T() const = delete; //Everithing which is not a bool (Everithing which  
                               //does not fit in the explicit overload) would  
                               //resolve to this operator and will fail.
};


int main()
{
    foo f;

    bool b = f; //OK
    int i = f;  //ERROR
    char c = f; //ERROR
    etc...
}

这篇关于显式bool操作符 - 不能返回,测试,初始化bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆