草庐IT

c++ - 在 switch 语句中从 int 到 enum 类的隐式转换

enumclasspid{Alpha,Beta,Gamma};intmain(){intpropId=2;switch(propId){casepid::Alpha:casepid::Beta:casepid::Gamma:break;}}以上片段在msvc2012中编译良好(并且有效)但在clang-3.4和g++-4.8中失败。这些需要static_cast(propId)在switch子句中使用。顺便说一下,没有显式转换的简单赋值,例如pida=propId;在每个编译器中给出错误。谁做对了? 最佳答案 标准第4条,“标准转换

C++构造函数隐式转换没有发生

我定义了一个A类classA{public:A(int){}private:A(constA&);A&operator=(constA&);};我认为既然我从int中给出了一个构造函数,隐式构造被授予......不管怎样AmyA(7);工作正常,g++在这一行给我:AmyA=7;以下错误:Test02.cpp:Infunction‘intmain(int,char**)’:Test02.cpp:5:3:error:‘A::A(constA&)’isprivateTest02.cpp:12:12:error:withinthiscontext相反,另一个编译器对这种转换很满意。真相在哪里

c++ - 显式调用类的强制转换运算符方法是否比强制转换更好?

如果一个类提供了一个方法operatorwchar_t*并且我在编译器无法自动推断转换的情况下使用此类的实例,显式调用转换运算符方法而不是实际执行转换是否被认为是不好的?例如:x.doStuff(o.operatorwchar_t*())对比x.doStuff(static_cast(o))第一个选项是坏的/错误的,还是调用一个运算符方法是完全可以接受的? 最佳答案 至少这是不寻常的……特别是因为第二个结构非常地道。即使使用普通的转换运算符也应该更好。 关于c++-显式调用类的强制转换运

c++ - 复制初始化期间不会发生 std::string 的隐式构造

我正尝试在main()函数中复制初始化我的CObj类:#include#includeclassCObj{public:CObj(std::stringconst&str):m_str(str){std::cout但是,即使std::string是从charconst*隐式构造的,CObjobj="hello"行也无法编译>。根据我在这里的理解,这应该有效。有什么理由不这样做吗?如果我这样做,它会起作用:CObjobj=std::string("hello"); 最佳答案 文字"Hello"的类型为constchar[6]:为了调用

12.鸿蒙HarmonyOS App(JAVA) page的隐式跳转

跳转到指定Page的指定AbilitySlice MainAbilitySlice按钮触发事件: btn.setClickedListener(component->{      Intent_intent=newIntent();      Operationoperation=newIntent.OperationBuilder()          .withBundleName(getBundleName())          .withAction(SecondPageAbility.ACTION_TARGET)          .withAbilityName(SecondPa

c++ - 为什么(删除的)复制构造函数优于隐式转换?

考虑下面的代码:structBar{};structFoo{Foo()=default;Foo(constBar&){}Foo(constFoo&)=delete;//IMPLICITconversiontoBaroperatorBar(){return{};}};intmain(){Foof1;Foof2(static_cast(f1));//thisisOKFoof3(f1);//doesnotcompile,whynotimplicitconversionto`Bar`?}类Bar有一个用户定义的转换运算符到Foo,它接受Bar&。然而,在main的最后一行,我希望Foof1被转

c++ - std::map emplace 因显式构造函数而失败

classA{public:explicitA(intx){}};vectorv;v.push_back(1);//compilererrorsincenoimplicitconstructorv.emplace_back(1);//callsexplicitconstructor以上来自video大卫·斯通。我不明白的是为什么emplace_back调用显式构造函数?我在C++标准中看不到任何内容使这合法。只有在听了DavidStone的youtube视频后,我发现了这件事。现在,我对std::map进行同样的尝试。mapm;m.insert(pair(1,2));//compile

c++ - template<> 用于成员枚举的显式特化

根据17.7.3[temp.expl.spec]第5段(N4659),...Membersofanexplicitlyspecializedclasstemplatearedefinedinthesamemannerasmembersofnormalclasses,andnotusingthetemplatesyntax.Thesameistruewhendefiningamemberofanexplicitlyspecializedmemberclass.However,templateisusedindefiningamemberofanexplicitlyspecializedm

c++ - lambda 表达式中引用捕获和非显式捕获的 constexpr 变量之间的区别

这个问题Accesstoconstexprvariableinsidelambdaexpressionwithoutcapturing回答了为什么下面示例中的ref-capture不是严格必要的。但另一方面,如果它被捕获,则会出现错误。错误似乎是由foo()的递归性质触发的。templateconstexprintbar(constT&x){//NOK//constexprintbar(Tx){//OKreturnx;}templateintfoo(constT&l){constexprautox=l()-1;autoy=[&]{returnbar(x);};//ifref-captu

c++ - 模板模板参数的显式匹配

考虑这个函数:templateclassC,classT,classAlloc>voidfoo(C&container){std::cout函数foo()接受std::vector,但它也接受std::map在C++17中因为Compare和Allocator有默认值。备注std::map在C++14中失败。我怎样才能制作foo()只接受只有2个模板参数的模板,所以失败并显示std::map在C++17中? 最佳答案 HowcanImakefooonlyaccepttemplateswithexactlyonly2templatep