This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#include<iostream>
usingnamespacestd;
structA{
A(int){}
operatorbool()const{returntrue;}
};
structB{
explicitB(int){}
explicitoperatorbool()const{returntrue;}
};
voiddoA(Aa){}
voiddoB(Bb){}
intmain(){
Aa1(1);// OK:直接初始化
Aa2=1;// OK:复制初始化
Aa3{1};// OK:直接列表初始化
Aa4={1};// OK:复制列表初始化
Aa5=(A)1;// OK:允许 static_cast 的显式转换
doA(1);// OK:允许从 int 到 A 的隐式转换
if(a1)
;// OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
boola6(a1);// OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
boola7=a1;// OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
boola8=static_cast<bool>(a1);// OK :static_cast 进行直接初始化
Bb1(1);// OK:直接初始化
// B b2 = 1; // 错误:被 explicit
// 修饰构造函数的对象不可以复制初始化
Bb3{1};// OK:直接列表初始化
// B b4 = { 1 }; // 错误:被 explicit
// 修饰构造函数的对象不可以复制列表初始化
Bb5=(B)1;// OK:允许 static_cast 的显式转换
// doB(1); // 错误:被 explicit
// 修饰构造函数的对象不可以从 int 到 B 的隐式转换
if(b1)
;// OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool
// 的按语境转换
boolb6(b1);// OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B