草庐IT

if-cases

全部标签

c++ - 带有非类型模板参数的 std::enable_if

您将如何在std::enable_if中使用非类型模板参数比较?我无法弄清楚如何再次执行此操作。(我曾经有过这个工作,但我丢失了代码所以我无法回头看,我也找不到我在其中找到答案的帖子。)预先感谢您对此主题的任何帮助。templateclassMatrix{statictypenamestd::enable_if>::typeIdentity(){Matrixret;for(inty=0;y编辑:修复了评论中指出的缺失括号。 最佳答案 这完全取决于您想在无效代码上引发哪种错误/失败。这是一种可能性(撇开明显的static_assert

c++ - 有没有更好的方法来重写这个丑陋的 switch 和 if 语句组合?

本质上,我有一个Gamma探测器系统,每个探测器被分成4个晶体,在只有2个晶体命中的情况下,我们可以确定该对是垂直还是平行于产生react的平面Gamma射线。在为此编写逻辑的过程中,我写了一个巨大而丑陋的开关语句组合,在每个探测器中检查晶体编号的组合(它们在整个探测器及其晶体阵列中是唯一的)。这是代码,包括有问题的函数。//TheParallelandPerpendiculardesignationsareusedinadditiontotheDouble//designationforthe90degreedetectorsifwegetadiagonalscatterinthos

c++ - Direct2D : Unhandled Exception In WM_RESIZE switch case

我正在创建一个SimpleDirect2DApplication.不幸的是它给未处理的异常。发生的函数:voidDemoApp::OnResize(UINTwidth,UINTheight){if(m_pRenderTarget)Resize(D2D1::SizeU(width,height));}}调用OnResize()的代码片段是:DemoApp*pDemoApp=reinterpret_cast(static_cast(::GetWindowLongPtrW(hwnd,GWLP_USERDATA)));boolwasHandled=false;if(pDemoApp){swit

c++ - 无法编译包含 "if constexpr"的函数模板实例化

抱歉标题含糊不清,但我想不出更好的标题。我写了一个函数来压扁容器:templatevoidflatten(constContainer&container,OutItres){ifconstexpr(std::is_convertible_v::value_type>){for(constauto&value:container){*res=value;++res;}}else{for(constauto&subContainer:container)flatten(subContainer,res);}}我希望它像这样使用:vector>test={{1},{2,3,4},{5,6}

c++ - enable_if 类型不属于某个模板类

TLDR:请参阅最后一段。我为几个模板类定义了一个operator&,如下所示:templatestructClass{Class(Tconst&t){}};templatestructClassAnd{ClassAnd(T_Lhsconst&lhs,T_Rhsconst&rhs){}};templateClassAnd,T_Rhs>operator&(Classconst&lhs,T_Rhsconst&rhs){returnClassAnd,T_Rhs>(lhs,rhs);}templateClassAnd,T_Rhs>operator&(ClassAndconst&lhs,T_Rh

c++ - STL std::remove_if 编译器失败

我无法让std::remove_if进行编译,如您所见,我选择了另一种工作正常的手摇曲柄方法,编译器错误位于列表底部的代码之后。任何帮助将不胜感激。谢谢,汤姆#include#include#include#include#include////Findthelargestcompoundwordcomposed//ofsub-wordsfromalist.////-readlistfromfile.////PsuedoCode:////1.ReadNextWordfromFile.//2.Searchinlistforwordformedfromword.//3.ifFoundinL

c++ - 模板类特化 enable_if 和默认值

鉴于以下templatestructtest{};template::value>::type>structtest{};假设some_trait::value是true,enable_if::type是void,然后选择专业。但是,当出现以下情况时,我的查询与选择有关。template::value,T>::type>structtest{};当enable_if的第二个非空模板参数时为::type提供,非特化模板被选中,即使some_trait::value是true,作为::type是T而不是void,所以没有匹配主模板中的默认值。我的问题是标准中的哪个位置描述了选择模板的顺序,

STL - 找到所有的 find_if 的替代版本,而不仅仅是第一个?

是否有std::find_if的替代版本返回所有找到的元素的迭代器,而不仅仅是第一个元素?例子:boolIsOdd(inti){return((i%2)==1);}std::vectorv;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);std::vector::iteratorit=find_if(v.begin(),v.end(),IsOdd);for(;it!=v.end();++it){std::cout 最佳答案 您可以只使用for循环:for(std

c++ - boost MPL : Call a (member) function only if it exists

我有一个类A,它有一个模板参数T。有些用例中类T提供函数func1(),有些用例中T不提供它。A中的函数f()应该调用func1(),前提是它存在。我认为这应该可以通过boostmpl实现,但我不知道如何实现。这里有一些伪代码:templateclassA{voidf(Tparam){if(T::func1isanexistingfunction)param.func1();}};如果是else-case会更好:templateclassA{voidf(Tparam){if(T::func1isanexistingfunction)param.func1();elsecout

c++ - 如何从 if 语句中获取类模板的实例? (C++)

假设我有一个类模板,它有一个成员pData,它是一个任意类型T的AxB数组。templateclassX{public:intA;intB;T**pData;X(inta,intb);~X();voidprint();//functionwhichprintspDatatoscreen};templateX::X(inta,intb){//constructorA=a;B=b;pData=newT*[A];for(inti=0;i>type;if(type=="int"){XXArray(a,b);}elseif(type=="char"){XXarray(a,b);}else{std