草庐IT

c++ - STL 中的 VS 编译器错误 C2752 ("more than one partial specialization matches")

不知何故,我喜欢这些显示(基本?)问题的“最短”程序。在VS2008中测试一些模板代码时出现了这个错误(它也已在VS2010和VS2012中得到确认,见下文):c:\programfiles(x86)\microsoftvisualstudio9.0\vc\include\xmemory(225):errorC2752:'std::_Ptr_cat_helper':morethanonepartialspecializationmatchesthetemplateargumentlistwith[_T1=constfloat(**),_T2=constfloat(**)]我可以将问题归

c++ - 当调用来自相同的非 const 版本重载成员函数时,是否可以删除 const 限定符?

例如:structB{};structA{constB&findB()const{/*somenontrivialcode*/}//B&findB(){/*thesamenontrivialcode*/}B&findB(){constA&a=*this;constB&b=a.findB();returnconst_cast(b);}};问题是我想避免在常量findB和非常量findB成员函数中重复相同的逻辑。 最佳答案 是的,您可以将对象转换为const,调用const版本,然后将结果转换为非const:returnconst_ca

c++ - 在没有 USES_CONVERSTION 的情况下从 const char * 转换为 LPTSTR

我正在尝试将constchar*转换为LPTSTR。但我不想使用USES_CONVERSION来执行它。以下是我使用USES_CONVERSION进行转换的代码。有没有办法使用sprintf或tcscpy等进行转换?USES_CONVERSION;jstringJavaStringVal=(somevaluepassedfromotherfunction);constchar*constCharStr=env->GetStringUTFChars(JavaStringVal,0);LPTSTRlpwstrVal=CA2T(constCharStr);//Idonotwanttouset

C++98/03 引用折叠和 cv 限定符

下面的代码编译(gcc4.7.2或icc13)并产生“12”输出。这意味着const预选赛被删除,我。即,f具有参数类型int&.为什么会这样?据我了解,根据§14.3.1.4:Ifatemplate-argumentforatemplate-parameterTnamesatype“referencetocv1S”,anattempttocreatethetype“referencetocv2T”createsthetype“referencetocv12S”,wherecv12istheunionofthecv-qualifierscv1andcv2.Redundantcv-qual

c++ - OpenCV/C++ 中的 MATLAB sub2ind/ind2sub

OpenCV中是否有任何函数等同于MATLAB的sub2ind和ind2sub函数?我的C++应用程序需要这两个函数。如果OpenCV缺少这些功能,是否有提供等效功能的C++库? 最佳答案 你可以自己写:intsub2ind(constintrow,constintcol,constintcols,constintrows){returnrow*cols+col;}voidind2sub(constintsub,constintcols,constintrows,int&row,int&col){row=sub/cols;col=s

c++ - 为什么编译器选择下面的模板版本?

编译器使用模板版本来计算t=max(a,b)和max(t,c)。欢迎从标准中引用任何支持这一点的内容。#includetemplateinlineTconst&max(Tconst&a,Tconst&b){std::coutinlineTconst&max(Tconst&a,Tconst&b,Tconst&c){returnmax(max(a,b),c);}inlineintconst&max(intconst&a,intconst&b){std::coutThecodeprintstemplatetemplate7 最佳答案 ma

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 reference 或 by value 传递 int,有什么区别吗?

这个问题在这里已经有了答案:Isitcounter-productivetopassprimitivetypesbyreference?[duplicate](7个答案)关闭7年前。当我将int和double之类的原语传递给函数时,是通过constreference传递它们更好,还是通过值传递它们更好(假设我不更改变量的值)?intgetValueFromArray(intindex){//returnthevaluefromthearray}intgetValueFromArray(constint&index){//returnthevaluefromthearray}谢谢

c++ - 是使用T const&还是T&&

我很好奇,一般来说,您是否要对从C++11开始的模板化函数参数使用T&&(通用引用)而不是经典的Tconst&(左值引用)。我特别好奇的是,如果您还想处理r值引用,您将如何避免被迫丢失const的事实;有办法解决这个问题吗? 最佳答案 “丢掉const”是没有问题的。如果参数是cv限定的,则T将使用cv限定符推导。例如,如果参数是conststd::string类型的右值,则T将被推导为conststd::string。您不可能通过使用转发引用来违反常量正确性。如果可以,那将是一个主要的语言缺陷。至于何时应使用转发引用的更广泛问题,

c++ - 在编译时填充 std::array 和 const_cast 可能的未定义行为

已知std::array::operator[]由于C++14是constexpr,请参见下面的声明:constexprconst_referenceoperator[](size_typepos)const;但是,它也是const限定的。如果您想使用std::array的下标运算符以便在编译时为数组赋值,这会产生影响。例如考虑以下用户文字:templatestructFooLiteral{std::arrayarr;constexprFooLiteral():arr{}{for(inti(0);i如果您尝试声明类型为FooLiteral的constexpr变量,上述代码将无法编译。这