隐式转换为布尔值,并与布尔文字进行比较 [英] Implicit conversion to boolean and comparison with boolean literals

查看:133
本文介绍了隐式转换为布尔值,并与布尔文字进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回答了此问题,关于用户定义的转换主题 bool 以及如何禁用其他转换:

I was answering this question, about the topic of user defined conversions to bool and how to disable other conversions:

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...
}

为什么 if(f == true)的条件失败(其中 f foo 。我自己尝试过,令我惊讶,因为与布尔文字的比较导致转换为 int (这是禁用),而不是 bool

Later the OP asked me why conditionals like if( f == true ) failed (Where f is a foo. I have tried that by myself and that surprises me because that comparison with boolean literals leads to a conversion to int (Which is disabled) instead of bool:

int main()
{
    foo f;

    if( f ); //OK, converts to bool
    if( f == true ); //ERROR: Conversion to int, which has been disabled
}  




prog.cpp:20:12:error:use的删除函数'foo :: operator T()
const [with T = int]'if(f == true);

............ ................................................. ................................................. .............................. ^

prog.cpp:20:12: error: use of deleted function ‘foo::operator T() const [with T = int]’ if( f == true);
..............................................................................................................................................^

我的问题是:布尔文字被定义为整数(像常见的C宏 #define true 1 #define false 0 ),为什么该比较导致int转换,而不是 bool

My question is: Are boolean literals defined as integers (Like the common C macros #define true 1 #define false 0), and if not, why that comparison lead to an int conversion instead of a bool?

我使用GCC4 。

-std = C ++ 11 http://ideone.com/8Ch5Jg =nofollow>这里是在ideone的行为的一个例子。

Here is an example of that behavior at ideone.

推荐答案

由于 foo 是类类型,当使用没有为该类类型专门定义的运算符时,应用特殊规则,请参阅[over.built] / 12

Since foo is a class type, special rules apply when using an operator that is not specifically defined for that class type, see [over.built]/12


对于每对提升的算术类型 L R ,存在以下形式的候选运算符函数:

For every pair of promoted arithmetic types L and R, there exist candidate operator functions of the form

LR operator*(L, R);
LR operator/(L, R);
LR operator+(L, R);
LR operator-(L, R);
bool operator<(L, R);
bool operator>(L, R);
bool operator<=(L, R);
bool operator>=(L, R);
bool operator==(L, R);
bool operator!=(L, R);

其中 LR 类型 L R 之间的算术转换。

where LR is the result of the usual arithmetic conversions between types L and R.

不是使用术语提升的算术类型:这需要促销 bool

Not the usage of the term promoted arithmetic types: This requires a promotion of bool.

A bool 被提升为 int ,请参阅[conv.prom] 6(整数促销)

A bool is promoted to an int, see [conv.prom]/6 (integral promotions)


bool类型的prvalue可以转换为 int false 变为零, true 变为一个。

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

因此,有以下形式的候选函数:


So there are candidate functions of the form

common_type<L,int> operator==(L, int);

其中 L 是提升的算术类型。但是,它们中有很多,例如

where L is a promoted arithmetic type. However, there are many of them, for example

common_type<int      , int> operator==(int      , int);
common_type<long     , int> operator==(long     , int);
common_type<long long, int> operator==(long long, int);

每个都需要用户定义的转换 foo 添加到提升的类型。我不清楚为什么这是不明确的,因为 foo 可以转换为任何一个。这在开放性问题中也有描述, CWG 954

Each of which require a user-defined conversion from foo to the promoted type. It is not clear to me why this isn't ambiguous, as foo can be converted to any of them. This is also described in the open issue CWG 954.

如果有多个非模板转换函数, g ++ 4.8.1 clang ++ 3.5 报告这种模糊性。 (应该注意,clang可能在此处有错误,请参见此示例,它工作正常with g ++ 4.8.1。)

If there are several non-template conversion functions, g++4.8.1 and clang++3.5 report this ambiguity. (It should be noted that clang might have a bug here, see this example which works fine with g++4.8.1.)

但是,没有

common_type<bool, int> operator==(bool, int);

bool / em>提升的算术类型。因此,从 foo bool 的转换将不会选择 p>

as bool is not a promoted arithmetic type. Therefore, the conversion from foo to bool will not be chosen for the expression

foo x;
x == true

这篇关于隐式转换为布尔值,并与布尔文字进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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