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 */;
for (;t;) /* statement */;
while (t) /* statement */;
do { /* block */ } while (t);
if(t)/ *声明* /;
for(; t;)/ * statement * /;
while(t)/ *声明* /;
做{/ * block * /} while(t);
Expressions
!t
t && t2
t || t2
t ? "true" : "false"
t && t2
t || T2
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 astd::unique_ptr
), it may be contextually converted. Also, the return value of aNullablePointer
's equality and inequality operators must be contextually convertible tobool
.在标准要求满足此概念的类型(例如std :: unique_ptr的指针类型)的任何地方,它可以在上下文中转换。此外,NullablePointer的相等和不等式运算符的返回值必须在上下文中可转换为bool。
-
std::remove_if(first, last, [&](auto){ return t; });
In any algorithm with a template parameter called
Predicate
orBinaryPredicate
, the predicate argument can return aT
.在任何带有名为Predicate或BinaryPredicate的模板参数的算法中,谓词参数都可以返回T.
-
In any algorithm with a template parameter calledstd::sort(first, last, [&](auto){ return t; });
Compare
, the comparator argument can return aT
.
std :: sort(first,last,[&](auto){return t;}); 在任何带有名为Compare的模板参数的算法中,比较器参数都可以返回T.
Do be aware that a mix of const and non-const conversion operators can cause confusion:
请注意,const和非const转换运算符的混合可能会导致混淆:
- Why doesn't explicit bool() conversion happen in contextual conversion?
- Why does the
explicit operator bool
not in effect as expected?
为什么在上下文转换中没有发生显式的bool()转换?
为什么显式运算符bool没有按预期生效?
#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 */;
for (;t;) /* statement */;
while (t) /* statement */;
do { /* block */ } while (t);
if(t)/ *声明* /;
for(; t;)/ * statement * /;
while(t)/ *声明* /;
做{/ * block * /} while(t);
Expressions
!t
t && t2
t || t2
t ? "true" : "false"
t && t2
t || T2
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 astd::unique_ptr
), it may be contextually converted. Also, the return value of aNullablePointer
's equality and inequality operators must be contextually convertible tobool
.在标准要求满足此概念的类型(例如std :: unique_ptr的指针类型)的任何地方,它可以在上下文中转换。此外,NullablePointer的相等和不等式运算符的返回值必须在上下文中可转换为bool。
-
std::remove_if(first, last, [&](auto){ return t; });
In any algorithm with a template parameter called
Predicate
orBinaryPredicate
, the predicate argument can return aT
.在任何带有名为Predicate或BinaryPredicate的模板参数的算法中,谓词参数都可以返回T.
-
In any algorithm with a template parameter calledstd::sort(first, last, [&](auto){ return t; });
Compare
, the comparator argument can return aT
.
std :: sort(first,last,[&](auto){return t;}); 在任何带有名为Compare的模板参数的算法中,比较器参数都可以返回T.
Do be aware that a mix of const and non-const conversion operators can cause confusion:
请注意,const和非const转换运算符的混合可能会导致混淆:
- Why doesn't explicit bool() conversion happen in contextual conversion?
- Why does the
explicit operator bool
not in effect as expected?
为什么在上下文转换中没有发生显式的bool()转换?
为什么显式运算符bool没有按预期生效?