草庐IT

c++ - g++ 和 clang++ - 删除重载转换运算符歧义获取的指针

我试图发布此代码作为对thisquestion的回答,通过制作这个指针包装器(替换原始指针)。这个想法是将const委托(delegate)给它的指针,这样filter函数就不能修改值。#include#includetemplateclassmy_pointer{T*ptr_;public:my_pointer(T*ptr=nullptr):ptr_(ptr){}operatorT*&(){returnptr_;}operatorTconst*()const{returnptr_;}};std::vector>filter(std::vector>const&vec){//*vec.

c++ - 重载构造函数之间的歧义

如果我尝试实例化MyFooC,有人可以向我解释为什么在下面的代码中我会收到对Foo中重载构造函数的模棱两可的调用吗?我的假设是用作构造函数参数的整数会被提升为unsignedint并得到解析,但这显然是不正确的。templateclassFoo{private:tm_Value;unsignedintm_Length;public:Foo(constt&Value):m_Value(Value),m_Length(0){}Foo(unsignedintLength):m_Value(static_cast(0)),m_Length(Length){}};intmain(){FooMyF

c++ - C++中的函数指针歧义

我有两个问题:Q1)函数名本身是指针吗??如果它们是指针,那么它们存储的是什么值?否则如果它们不是指针,那么,它们是什么以及其中存储了哪些值?如果我们认为函数名是指针。然后:voiddisplay(){...}intmain(){void(*p)();**p=display;//Works(justified**,becauseweareassigningonepointerintoanother)**p=&display;//Works(Notjustified**Iffunctionnameisapointer(letsaytype*),then&displayisofdataty

c++ - 为什么函数重载有歧义,而模板重载却没有歧义?

这个问题在这里已经有了答案:Thiscaseoftemplatefunctionoverloadingeludesmyunderstanding(1个回答)关闭7年前。为什么,在下面,调用了bar的实例化?没有歧义,而非模板重载函数foo是模棱两可的。nullptr也是一样的而不是NULL#includetemplatevoidbar(T*){std::coutvoidbar(typenameT::value_type*){std::cout(NULL);foo(NULL);//ambigous}编辑:要清楚。我期待foo过载是模棱两可的。我不明白为什么bar实例化bar时生成的重载不

c++ - 使用模板转换运算符解决歧义

我不得不做一个类似的代码:#includetemplatestructprobe{template::value&&!std::is_const::value,int>=0>operatorT&()const;template::value&&!std::is_const::value,int>=0>operatorT&&();template::value,int>=0>operatorTconst&()const;template::value,int>=0>operatorTconst&&()const;};structsome_type{};structother_type{}

c++ - 使用 boost::assign::list_of 构造 std::vector 时的歧义

这段代码:std::vector(boost::assign::list_of(1)(2)(3));给出错误:main.cpp:Inmemberfunction'void::RequestHandler::processRequest(Foo&,Bar,unsignedint,unsignedint*,constchar*,boost::shared_ptr&)':main.cpp:450:error:callofoverloaded'vector(boost::assign_detail::generic_list&)'isambiguous/4.4.2/bits/stl_vecto

c++ - 具有共享指针参数歧义的函数重载

我想制作重载函数,这些函数采用指向基类和派生类的共享指针。它似乎适用于引用和原始指针,但在额外派生类的情况下不适用于共享指针。查看示例代码:#includeclassBase{};classDerived:publicBase{};classExtraDerived:publicDerived{};boolIsBase(Base*){returntrue;}boolIsBase(Derived*){returnfalse;}boolIsBase(std::shared_ptr){returntrue;}boolIsBase(std::shared_ptr){returnfalse;}i

c++ - 模板运算符似乎因歧义而失败

这不是重复的。我检查了很多答案、常见问题解答和其他内容。这些都没有告诉我消息。这是简化的代码。这是获取和解释错误的最低限度。/***Polynomial.hpp********************************************************/namespaceModulus{//Forwarddeclarationofthetypesandnon-inlinetemplatefriendfunctions.templateclassPolynomial;templatePolynomialoperator+(Polynomialconst&p,Polyn

c++ - 使用带有 std::optional 参数的 std::function 重载歧义

这是一个简单的示例程序:usingfn_string=function;usingfn_optional_string=function&)>;voidfoo(fn_string){cout&){});//它有2个重载foo()--一个使用string的函数参数和另一个optional.为什么第二次调用foo()模棱两可?有没有简单的方法来修复它?没有Actor?更新以上是我试图解决的以下现实世界问题的过度简化示例,即:usingdelegate=variant,function,function&)>>;structfoo{voidadd_delegate(delegatefn){f

C++11:传递值参数初始化中转换构造函数和转换函数之间的歧义?

#includeusingnamespacestd;structY;structX{X(constY&){cout在上面,转换函数被我的编译器(gcc4.6.1)赋予了转换构造函数的优先权,但是在标准中它声明:User-definedconversionsareappliedonlywheretheyareunambiguous在这种情况下似乎存在歧义。谁能解释一下矛盾?我原以为上面的代码不会编译。我也很确定几年前ScottMeyers写了关于这个特定示例的文章,并说它无法编译。我错过了什么? 最佳答案 因为X构造函数需要一个con