jjzjj

overload-resolution

全部标签

c++ - 什么被认为是编译时分支?

这个问题在这里已经有了答案:Whatdocompilersdowithcompile-timebranching?(5个答案)关闭8年前。提供编译时分支的技术/c++语言功能是什么?第一次尝试枚举它们(我期待添加更正):重载解决方案:例如,选择“最佳”版本适合提供的参数voidF(X&arg);voidF(X&&arg);模板专门化:创建针对“特殊参数”运行的代码-一种对模板元编程和编译时递归至关重要的技术templatestructA{/*implementation*/};templatestructA{/*specificcode*/};SFINAE&expressionsfin

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++ - 重载解析和显式模板参数

以下代码prints"func2".为什么编译器将第二个模板视为更好的匹配,存在显式(未推导出的)模板参数?为什么没有歧义?如果引用C++标准,我将不胜感激。#includetemplatestructidentity{typedefTtype;};templatevoidfunc(T){std::coutvoidfunc(typenameidentity::type){std::cout(1);} 最佳答案 两个候选人都是可行的并且采用相同的参数,因此重载解决过程回退到最后一个决胜局:函数模板的偏序[temp.func.order

c++ - "Ambiguous conversion sequence"- 这个概念的目的是什么?

在N465916.3.3.1隐式转换序列中说10Ifseveraldifferentsequencesofconversionsexistthateachconverttheargumenttotheparametertype,theimplicitconversionsequenceassociatedwiththeparameterisdefinedtobetheuniqueconversionsequencedesignatedtheambiguousconversionsequence.Forthepurposeofrankingimplicitconversionsequen

c++ - C++如何考虑常量性,模板化性和泛型性来解析专用模板?

我有以下代码,可能看起来有些费解,但来自真实代码:#includeusingnamespacestd;templatevoidfoo(Hrm&h,A&a){coutclassHrg>voidfoo(Hrg&h,int&a){coutclassHrg>voidfoo(Hrg&h,constint&a){coutstructwhat;templatestructwhat{};templatestructwhat{};intmain(){whatwt;whatwf;inti=5;constint&ri=i;foo(wt,i);//1)genericfoo(wf,i);//2)speciali

c++ - 比较rdtsc clock和c++11 std::chrono::high_resolution_clock产生的时间测量结果

我正在尝试比较由c++11std::chrono::high_resolution_clock和下面的rdtsc_clock时钟测量的时间。从high_resolution_clock,我得到类似11000、3000、1000、0的结果。从rdtsc_clock,我得到134、15、91等。为什么他们的结果看起来如此不同?根据我的直觉,我相信rdtsc_clock正在呈现~accurate结果,对吗?templatestructrdtsc_clock{typedefunsignedlonglongrep;typedefstd::ratioperiod;typedefstd::chron

具有不可更改的通用引用函数模板的 C++ 重载解决方案

假设我的代码中某处是一个带有通用引用参数的函数foo,我无法更改它:templateautofoo(T&&t){std::cout现在我想为给定的类A重载foo,并确保对于A的任何限定符和引用类型重载叫做。为此,我可以强行为所有可能的条件提供重载(暂时忽略volatile):autofoo(A&a){std::coutDemo.然而,对于更多参数,这非常糟糕。或者,我可以按值传递,这似乎也适用于所有以前的情况:autofoo(Aa){std::coutDemo.但是现在需要复制大对象(至少原则上是这样)。是否有解决这些问题的优雅方法?请记住我无法更改通用引用功能,因此SFINAE等是不

C++ 重载解析、用户定义转换和函数模板

对于g++3.4和4.7,我观察到以下奇怪的行为:如果需要用户定义的转换,则函数模板不匹配,而普通函数会。我在C++98标准中找不到相应的规则。g++是否正确(正如我假设的那样)?或者这是一个错误?templateintx(auto_ptr_refp){return1;}//thiswouldmatch/*intx(auto_ptr_refp){return2;}*/voiddummy(){cout()) 最佳答案 海湾合作委员会是正确的,templateargumentdeduction不考虑隐式转换。Typedeductiond

c++ - 直接初始化中的转换运算符

C++14标准(N4296)在8.5/17.6.1中说Iftheinitializationisdirect-initialization[...],constructorsareconsidered.Theapplicableconstructorsareenumerated,andthebestoneischosenthroughoverloadresolution.[...]Ifnoconstructorapplies,ortheoverloadresolutionisambiguous,theinitializationisill-formed.因此在直接初始化中,只考虑构造函

c++ - 使用哪个 : move assignment operator vs copy assignment operator

我似乎不明白为什么要使用移动赋值运算符:CLASSA&operator=(CLASSA&&other);//moveassignmentoperator结束了,复制赋值运算符:CLASSA&operator=(CLASSAother);//copyassignmentoperator移动赋值运算符仅采用r值引用,例如CLASSAa1,a2,a3;a1=a2+a3;在复制赋值运算符中,other可以是使用复制构造函数或移动构造函数的构造函数(如果other是用右值初始化的,它可以是移动构造的——如果move-constructor定义了——)。如果它是copy-constructed,我