草庐IT

c++ - 如何解决转换构造函数和普通构造函数之间的歧义?

我期待消除普通构造函数和自动转换构造函数之间的歧义。据我所知,可以通过将普通构造函数声明为explicit来部分消除这种歧义,因此编译器将避免将此构造函数用作转换构造函数,如下所示:#include#includeclassInteger{inti;public:explicitInteger(constint_i):i(_i){}//NormalconstructorInteger(constint&ii):i(ii){}//conversionconstructoroperatorint(){returnint(i);}//Auto-conversionoperatorfriend

c++ - 名称查找歧义不一致

我试图理解为什么这个程序没有为i提供名称查找歧义:namespaceX{inti=1;}namespaceQ{namespaceP{inti=2;usingnamespaceX;}usingnamespaceP;intl=i;}intmain(){}如果我们像这样修改它,我们会得到名称查找歧义:namespaceX{inti=1;}namespaceP{inti=2;usingnamespaceX;}usingnamespaceP;intl=i;intmain(){}我在这里所做的唯一更改是删除命名空间Q并将其内容放在全局命名空间中。我试过3种不同的编译器:GCC和Clanghttp:

c++ - C++ 中用于自动将对象转换为 "printable"格式的重载歧义

我正在尝试编写一个接受输入的函数。如果该输入可以直接通过管道传输到流(例如使用std::cout),它会原封不动地返回输入。否则,它会尝试将输入转换为字符串,并返回该字符串。我有以下代码://UsesSFINAEtodeterminewhichoverloadtocall//See:https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error//Basically,make_printabledetectswhetheranobjectcanbedirectlypipedtoastream.//Ifitcan'tb

c++ - 名称查找中的重载解析/歧义(哪一个)

$7.3.3/14(C++03)structA{intx();};structB:A{};structC:A{usingA::x;intx(int);};structD:B,C{usingC::x;intx(double);};intf(D*d){returnd->x();//ambiguous:B::xorC::x}“f”中代码中的注释表明可以预期“B::x”或“C::x”之间存在歧义。然而,在使用g++(ideone)或Comeau编译时,错误略有不同。这些错误不是指示B::x或C::x中的歧义,而是指示A是D的歧义基这一事实prog.cpp:Infunction‘intf(D*)

c++ - 模板实例化歧义

我正在玩弄一个templatedimplementation一个FSM并且遇到如下歧义:/home/permal/code/FSM/Test/../FSM/dist/include/FSM.h:Ininstantiationof‘voidfsm::FSM::Event(std::unique_ptr)[withEventType=AddEvent;FSMBaseState=EventBaseState]’:/home/permal/code/FSM/Test/test.cpp:83:44:requiredfromhere/home/permal/code/FSM/Test/../FSM

c++ - 使用 std::initializer_list 构造函数而不产生歧义?

我有一个类叫做Shape,它可以从任何可迭代对象和一个名为Array的类中初始化,其中只包含一个Shape.但是,当我尝试初始化Array时,我遇到了无法解释的编译错误。:classShape{public:templateShape(Iteratorfirst,Iteratorlast):m_shape(first,last){}templateShape(constIterable&shape):Shape(shape.begin(),shape.end()){}templateShape(std::initializer_listshape):Shape(shape.begin(

C++ 隐式转换和重载函数调用中的歧义

我面临以下问题:我有一个类V(比如一个vector),我可以从中生成两个类:CI和I(想想const_iterator和迭代器)。如果我有一个constV,那么我只能生成CI(再次想到iterator和const_iterator)。本质上,我想用(CIci)替换(constV&v),用(Ii)替换(V&v)。此外,我仍然希望能够将Vobj直接传递给需要I或CI的函数,从而实现从V和constV到CI和I的隐式转换。我面临的问题是,虽然重载函数可以区分(constV&v)和(V&v),但当我传递Vobj时,它们无法“区分”(CIci)和(Ii)。在代码中:structV{};struc

c++ - 模板函数调用歧义是如何解决的?

我想做一个函数conj仅当参数类型不是std::complex时才会应用.我可以使用enable_if,但我需要这样做吗?如果我有以下内容:namespace{templateTconj(Tx){returnx;}}我们已经在stdtemplatestd::complexconj(std::complexx);会调用conj(z)吗?其中z是std::complex被解析为标准版本(因为它是“更好”的匹配?) 最佳答案 Willacalltoconj(z)wherezisstd::complexberesolvedtothestdv

c++ - 重载函数歧义

首先我要指出,这是我的第一个stackoverflow问题,所以请多多包涵。我在重载C++中的函数时遇到了一些问题。我正在尝试使用以下原型(prototype)创建一个函数:voidpush_at_command(std::string,std::vector,int);voidpush_at_command(std::string,std::vector,std::vector,int);voidpush_at_command(std::string,std::vector,std::vector,std::vector,int);voidpush_at_command(std::s

c++ - 以不同的方式克服菱形歧义

我知道菱形继承(钻石问题)和使用虚拟基类解决它的方法。我试图以不同的方式解决菱形继承(钻石问题)但没有成功。我不知道为什么。#includeusingnamespacestd;classA{public:voiddisplay(){cout由于不继承私有(private)成员,D类不会从C继承任何函数,当D类继承B类和C类时,D中应该只有1个display()函数。但是当我尝试访问display()函数使用类D的对象,显示了同样的问题,显示函数不明确。 最佳答案 “不继承私有(private)成员”是一个错误的前提,这不是Java。