我何时可以在没有演员的情况下使用显式运算符bool?

时间:2023-02-07 02:57:08

My class has an explicit conversion to bool:

我的班级明确转换为bool:

struct T {
    explicit operator bool() const { return true; }
};

and I have an instance of it:

我有一个例子:

T t;

To assign it to a variable of type bool, I need to write a cast:

要将它分配给bool类型的变量,我需要编写一个强制转换:

bool b = static_cast<bool>(t);
bool b = bool(t);
bool b(t);  // converting initialiser
bool b{static_cast<bool>(t)};

I know that I can use my type directly in a conditional without a cast, despite the explicit qualifier:

我知道我可以直接在没有强制转换的条件中使用我的类型,尽管有明确的限定符:

if (t)
    /* statement */;

Where else can I use t as a bool without a cast?

我还能在没有演员的情况下使用t作为布尔?

1 个解决方案

#1


28  

The standard mentions places where a value may be "contextually converted to bool". They fall into four main groups:

该标准提到了值可能“在上下文中转换为bool”的位置。它们分为四大类:

Statements

  • if (t) /* statement */;
    
  • if(t)/ *声明* /;

  • for (;t;) /* statement */;
    
  • for(; t;)/ * statement * /;

  • while (t) /* statement */;
    
  • while(t)/ *声明* /;

  • do { /* block */ } while (t);
    
  • 做{/ * block * /} while(t);

Expressions

  • !t
    
  • t && t2
    
  • t && t2

  • t || t2
    
  • t || T2

  • t ? "true" : "false"
    
  • t? “真假”

Compile-time tests

The operator needs to be constexpr for these:

运营商需要为这些:constexpr:

  • static_assert(t);
    
  • noexcept(t)
    
  • if constexpr (t)
    
  • 如果constexpr(t)

Algorithms and concepts

  • NullablePointer T
    

    Anywhere the Standard requires a type satisfying this concept (e.g. the pointer type of a std::unique_ptr), it may be contextually converted. Also, the return value of a NullablePointer's equality and inequality operators must be contextually convertible to bool.

    在标准要求满足此概念的类型(例如std :: unique_ptr的指针类型)的任何地方,它可以在上下文中转换。此外,NullablePointer的相等和不等式运算符的返回值必须在上下文中可转换为bool。

  • std::remove_if(first, last, [&](auto){ return t; });
    

    In any algorithm with a template parameter called Predicate or BinaryPredicate, the predicate argument can return a T.

    在任何带有名为Predicate或BinaryPredicate的模板参数的算法中,谓词参数都可以返回T.

  • std::sort(first, last, [&](auto){ return t; });
    
    In any algorithm with a template parameter called Compare, the comparator argument can return a T.
  • std :: sort(first,last,[&](auto){return t;});  在任何带有名为Compare的模板参数的算法中,比较器参数都可以返回T.

(source1, source2)


Do be aware that a mix of const and non-const conversion operators can cause confusion:

请注意,const和非const转换运算符的混合可能会导致混淆:

#1


28  

The standard mentions places where a value may be "contextually converted to bool". They fall into four main groups:

该标准提到了值可能“在上下文中转换为bool”的位置。它们分为四大类:

Statements

  • if (t) /* statement */;
    
  • if(t)/ *声明* /;

  • for (;t;) /* statement */;
    
  • for(; t;)/ * statement * /;

  • while (t) /* statement */;
    
  • while(t)/ *声明* /;

  • do { /* block */ } while (t);
    
  • 做{/ * block * /} while(t);

Expressions

  • !t
    
  • t && t2
    
  • t && t2

  • t || t2
    
  • t || T2

  • t ? "true" : "false"
    
  • t? “真假”

Compile-time tests

The operator needs to be constexpr for these:

运营商需要为这些:constexpr:

  • static_assert(t);
    
  • noexcept(t)
    
  • if constexpr (t)
    
  • 如果constexpr(t)

Algorithms and concepts

  • NullablePointer T
    

    Anywhere the Standard requires a type satisfying this concept (e.g. the pointer type of a std::unique_ptr), it may be contextually converted. Also, the return value of a NullablePointer's equality and inequality operators must be contextually convertible to bool.

    在标准要求满足此概念的类型(例如std :: unique_ptr的指针类型)的任何地方,它可以在上下文中转换。此外,NullablePointer的相等和不等式运算符的返回值必须在上下文中可转换为bool。

  • std::remove_if(first, last, [&](auto){ return t; });
    

    In any algorithm with a template parameter called Predicate or BinaryPredicate, the predicate argument can return a T.

    在任何带有名为Predicate或BinaryPredicate的模板参数的算法中,谓词参数都可以返回T.

  • std::sort(first, last, [&](auto){ return t; });
    
    In any algorithm with a template parameter called Compare, the comparator argument can return a T.
  • std :: sort(first,last,[&](auto){return t;});  在任何带有名为Compare的模板参数的算法中,比较器参数都可以返回T.

(source1, source2)


Do be aware that a mix of const and non-const conversion operators can cause confusion:

请注意,const和非const转换运算符的混合可能会导致混淆: