草庐IT

c++ - 显式赋值与隐式赋值

我正在阅读C++教程,但实际上并没有给我带来两者之间的区别(除了语法)。这是教程中的引述。Youcanalsoassignvaluestoyourvariablesupondeclaration.Whenweassignvaluestoavariableusingtheassignmentoperator(equalssign),it’scalledanexplicitassignment:intnValue=5;//explicitassignmentYoucanalsoassignvaluestovariablesusinganimplicitassignment:intnValu

c++ - 检查类型是否可以显式转换

如何确定(本着精神)一种类型是否可显式转换为另一种类型?例如,我想检查F::explicitoperatordoubleconst&()const;的存在。对于一些class/structF,但同时,F不应显式转换为float或longdouble(类似于pred::value&&!pred::value&&!pred::value)。请注意,std::is_convertible::value检查“如果From可以使用隐式转换转换为To”。但我想确定是否有显式转换运算符。而且,如果可能的话,“如何确定类型From是否可转换为即对类型To的引用?”? 最佳答

c++ - 检查类型是否可以显式转换

如何确定(本着精神)一种类型是否可显式转换为另一种类型?例如,我想检查F::explicitoperatordoubleconst&()const;的存在。对于一些class/structF,但同时,F不应显式转换为float或longdouble(类似于pred::value&&!pred::value&&!pred::value)。请注意,std::is_convertible::value检查“如果From可以使用隐式转换转换为To”。但我想确定是否有显式转换运算符。而且,如果可能的话,“如何确定类型From是否可转换为即对类型To的引用?”? 最佳答

c++ - 大括号初始值设定项列表中是否允许显式转换运算符?

以下代码可以使用GCC4.9.2编译,但不能使用Clang3.5.0:#includeclassFoo{public:explicitoperatorstd::string()const;};std::stringbar{Foo{}};//Worksing++,failsinclang++std::stringbaz(Foo{});//Worksinbothclang++说:foo.cpp:9:13:error:nomatchingconstructorforinitializationof'std::string'(aka'basic_string')std::stringbar{F

c++ - 大括号初始值设定项列表中是否允许显式转换运算符?

以下代码可以使用GCC4.9.2编译,但不能使用Clang3.5.0:#includeclassFoo{public:explicitoperatorstd::string()const;};std::stringbar{Foo{}};//Worksing++,failsinclang++std::stringbaz(Foo{});//Worksinbothclang++说:foo.cpp:9:13:error:nomatchingconstructorforinitializationof'std::string'(aka'basic_string')std::stringbar{F

c++ - Visual Studio 2017 是否需要显式移动构造函数声明?

使用VisualStudio2015可以成功编译以下代码,但使用VisualStudio2017编译失败。VisualStudio2017报告:errorC2280:“std::pair::pair(conststd::pair&)”:attemptingtoreferenceadeletedfunction代码#include#includestructNode{std::unordered_map>map_;//UncommentingthefollowingtwolineswillpassVisualStudio2017compilation//Node(Node&&o)=def

c++ - Visual Studio 2017 是否需要显式移动构造函数声明?

使用VisualStudio2015可以成功编译以下代码,但使用VisualStudio2017编译失败。VisualStudio2017报告:errorC2280:“std::pair::pair(conststd::pair&)”:attemptingtoreferenceadeletedfunction代码#include#includestructNode{std::unordered_map>map_;//UncommentingthefollowingtwolineswillpassVisualStudio2017compilation//Node(Node&&o)=def

c++ - 为什么在上下文转换中不发生显式 bool() 转换

如果下面的测试程序#includeclassA{public:A(){}explicitoperatorbool()const{std::cout运行,输出为intA::operatorint()bool()intA::operatorint()int()而不是boolA::operator_Bool()bool()intA::operatorint()int()我的预期(以及如果您取消注释注释部分会得到什么)。那么问题是,转换为非const-int优先于转换为const-bool的规则是什么? 最佳答案 在对引用绑定(bind)执

c++ - 为什么在上下文转换中不发生显式 bool() 转换

如果下面的测试程序#includeclassA{public:A(){}explicitoperatorbool()const{std::cout运行,输出为intA::operatorint()bool()intA::operatorint()int()而不是boolA::operator_Bool()bool()intA::operatorint()int()我的预期(以及如果您取消注释注释部分会得到什么)。那么问题是,转换为非const-int优先于转换为const-bool的规则是什么? 最佳答案 在对引用绑定(bind)执

c++ - 为什么双参数构造函数以显式关键字开头?

我和我的伙伴最近一直在阅读leveldb源代码。我们遇到了这个问题。在leveldbdb/skiplist.h文件,有一个构造函数声明:explicitSkipList(Comparatorcmp,Arena*arena);我知道带有单个参数的显式构造函数意味着构造函数参数没有隐式类型转换。但是带有显式关键字的双参数构造函数是什么意思?是C++11的新规则吗?谢谢。 最佳答案 使用C++11,您可以使用braced-init-lists代替其他一些表达式,这会有所不同。例如,您可以在return语句中使用它们:SkipListfoo