jjzjj

c++ - 在存在新的初始化序列的情况下,运算符重载决议如何工作?

给定以下代码:#include#includetemplateclassConvertProxy{Sourceconst*m_source;public:ConvertProxy(Sourceconst&source):m_source(&source){}templateoperatorDest()const{returnDest(m_source->begin(),m_source->end());}};templateConvertProxyconvert(Sourceconst&source){returnConvertProxy(source);}intmain(){std:

c++ - 以 nullptr 作为参数的函数重载决议

考虑下面的代码。虽然fun的两个重载都接受指针,但将nullptr传递给fun不会导致任何编译错误。然而,非常相似的函数bun无法编译。当我使用typeid(i).name()打印参数i的类型时(修改代码后只是为了打印)我得到相同的类型,只是int*。在fun情况下解决歧义但在bun情况下失败的规则是什么?提前致谢!#includestructFoo{intsth;};templatevoidfun(decltype(U::sth)*i){std::coutvoidfun(U*i){std::cout(nullptr);//bun(nullptr);-->callofoverloade

c++ - initializer_list 和默认构造函数重载决议

#include#includeusingnamespacestd;structY{};structX{X(initializer_list){cout这会打印出“boo”。为什么它不打印出“yay”?无论如何要区分以下两种结构:X()X{}或returnX();返回{};或voidg(constX&)g(X())g({})谢谢。 最佳答案 Isthereanywaytodifferentiatethefollowingtwoconstructions:没有。它们不是不同的结构。{}构造函数语法的主要目的是引入统一初始化,让初始化工

c++ - 为什么在这种情况下重载决议不明确?

我编写这段代码是为了检查类类型是否有begin功能。structfoo//asimpletypetocheck{intbegin(){return0;}};structFallback{intbegin(){return0;}};templatestructHasfuncBegin:T,Fallback{typedefcharone;typedefinttwo;templatestaticonecheck(int(X::*)()=&HasfuncBegin::begin);templatestatictwocheck(...);enum:bool{yes=sizeof(check())

c++ - 同级友元运算符似乎不参与重载决议

在编写使类能够根据模板参数为operator+提供重载的CRTP模板时,我发现类内友元运算符似乎不参与重载决议,如果没有的话arguments是它在其中定义的类的类型。归结:enumclassFooValueT{zero,one,two};classFoo{FooValueTval_;public:Foo(FooValueTx):val_(x){};Foo&operator+=(Fooother){val_=(FooValueT)((int)val_+(int)other.val_);return*this;}//overloadforFoo+Foo,FooValueT+FooandF

c++ - 模板化运算符重载决议,成员函数与非成员函数

在试用clang-3.4(从gi​​t编译)时,它无法编译我的一个项目,提示在解析重载运算符时存在歧义。我发现有两个模板化运算符,其中一个被声明为成员函数,另一个被声明为非成员函数,两者看起来同样匹配。以下SSCCE演示情况:#includestructostr{std::ostream&s;templateostr&operatorStream&operator该项目之前编译得很好,我用几个编译器(gcc4.5、4.6、4.7、4.8和clang3.3)再次检查了这个SSCCE,它们都在没有任何警告的情况下编译了它(-Wall-Wextra-pedantic)。所有编译器都设置为C+

c++ - 使用 const 引用的函数模板重载决议

我试图理解以下情况下的重载决议规则:templatevoidf(constT&x){std::coutvoidf(T&x){//ÜberladungVariante2std::cout输出是:voidf(T&)[T=int]voidf(constT&)[T=int]据我所知,第一次调用f(e1)会导致可行的功能voidf(constint&)voidf(int&)从中选择第一个,因为没有删除const限定。第二次调用f(e2)导致类型推导/可行函数voidf(constint&);//T->intfromfirsttemplateoverloadvoidf(constint&);//T

c++ - boost::variant 和函数重载决议

以下代码无法编译:#includeclassA{};classB{};classC{};classD{};usingv1=boost::variant;usingv2=boost::variant;intf(v1const&){return0;}intf(v2const&){return1;}intmain(){returnf(A{});}gcc和clang都提示:test1.cpp:Infunction‘intmain()’:test1.cpp:18:17:error:callofoverloaded‘f(A)’isambiguousreturnf(A{});^test1.cpp:1

c++ - 成员函数的 const 修饰符如何影响重载决议?

我有以下测试代码:#include#includeclassCString{public:CString(charconst*){}};classTestBed{public:voidComparison(CStringconst&){std::cout此代码无法编译,因为对Comparison()的调用不明确。我期待这种行为。但是,当我进行任一Comparison()重载时const,如:voidComparison(std::stringconst&)const或voidComparison(CStringconst&)const(但不是两者),代码编译并选择非常量重载。重载解决规

c++ - 为什么一个程序被拒绝为不明确的,可以通过重载决议来解决?

以下程序被gcc拒绝为有歧义:structAint{virtualvoidfoo(int);};structAstring{virtualvoidfoo(std::string);};structA:publicAint,publicAstring{};intmain(){std::strings;Aa;a.foo(s);return0;}>vt.cpp:Infunction‘intmain()’:vt.cpp:13:9:error:requestfor>member‘foo’isambiguous>a.foo(s);>^vt.cpp:5:34:note:candidatesare:v