草庐IT

if-cases

全部标签

c++ - std::enable_if 是如何工作的?

我刚刚问了这个问题:std::numeric_limitsasaCondition我了解std::enable_if将定义方法的返回类型的用法,有条件地导致方法编译失败。templatetypenamestd::enable_if::is_integer,void>::typefoo(constT&bar){isInt(bar);}我不明白的是第二个参数以及当std::enable_if声明为模板语句的一部分时看似毫无意义的赋值,如Rapptzanswer.template::value,int>::type=0>voidfoo(constT&bar){isInt();}

c++ - 使用不同的 enable_if 条件选择成员函数

我正在尝试根据类模板参数确定调用哪个版本的成员函数。我试过这个:#include#includetemplatestructPoint{voidMyFunction(typenamestd::enable_if::value,T>::type*=0){std::cout::value,float>::type*=0){std::coutintPoint;intPoint.MyFunction();PointfloatPoint;floatPoint.MyFunction();}我认为是说“如果T是int,则使用第一个MyFunction,如果T不是int,则使用第二个MyFunctio

c++ - 如何将 std::enable_if 与自推断返回类型一起使用?

C++14将具有可以根据返回值推断返回类型的函数。autofunction(){return"helloworld";}我可以将此行为应用于使用enable_if的函数吗?对于SFINAE的返回类型习语?例如,让我们考虑以下两个函数:#include#include//Thisfunctionischosenwhenanintegraltypeispassedintemplateautofunction(Tt)->typenamestd::enable_if::value>::type{std::coutautofunction(Tt)->typenamestd::enable_if:

c# - 在 'if' 语句中混淆使用逗号

我有这段C++代码:ihi=y[0]>y[1]?(inhi=1,0):(inhi=0,1);但它在C#中会是什么样子? 最佳答案 意思是这样的:if(y[0]>y[1]){inhi=1;ihi=0;}else{inhi=0;ihi=1;}或者用另一种方式(用C++)编写:inhi=(y[0]>y[1]);ini=!inhi; 关于c#-在'if'语句中混淆使用逗号,我们在StackOverflow上找到一个类似的问题: https://stackoverflo

c++ - 执行 remove_if() 后删除()

我创建了一个函数来遍历字符串vector并删除长度为3或更短的任何字符串。这是使用STL算法库的一课。我在函数工作时遇到了麻烦,但它不仅会删除长度为3或更短的字符串,而且还会将字符串“vector”附加到末尾。输出应该是Thistestvector其实是Thistestvectorvector"我该如何解决?/**usingremove_ifandcustomcallbackfunction,writeRemoveShortWords*thatacceptsavectorandremovesallstringsoflength3or*lessfromit.*shootfor2lines

c++ - 比 if else if else... 更好的方法用于线性插值

问题很简单。假设你有功能doubleinterpolate(doublex);并且你有一张表,其中包含已知x->y的map例如5157月18日1022注意:真正的表更大,这只是示例。所以对于8,您将返回18+((8-7)/(10-7))*(22-18)=19.3333333我发现的一个很酷的方法是http://www.bnikolic.co.uk/blog/cpp-map-interp.html(长话短说,它使用std::map,key=x,value=y表示x->y数据对)。如果有人问标题中的ifelseifelse是什么意思基本上是:if((x>=5)&&(x=7)&&x那么有没有

c++ - 无中断的 switch-case 语句

根据我正在阅读的这本书:Q如果我在switch-case语句中省略了break会发生什么?Abreak语句使程序执行能够退出switch构造。没有它,执行将继续评估以下case语句。假设我的代码看起来像switch(option}{case1:doA;case2:doB;default:doC;break;}这是否意味着如果我选择案例1,A和C就完成了。如果我选择案例2,B和C就完成了。如果我都不选择,那么只有C完成。如果是这样,如果我们在doC之后省略了break会发生什么。我认为这些都是不好的编程习惯,但我很好奇会发生什么来更深入地了解它是如何工作的。谢谢

带有 enable_if : different behaviour with g++ and clang 的 C++ 模板重载

在解决基类的模板化成员函数的重载时,我观察到g++(5.2.1-23)和clang(3.8.0)之间的不同行为,-std=c++14.#include#includestructBase{templateautoa(Tt)->void{std::coutstructDerived:publicBase{usingBase::a;templateautoa(Tt)->std::enable_if_t{std::coutd;d.a(1);//failswithg++,prints"true"withclangDerivedd2;d2.a(1);//failswithclang++,prin

c++ - 为什么 SFINAE (enable_if) 不适用于类模板的成员函数?

#includestructA{};structB{};templatestructFoo{typenamestd::enable_if::value>::typebar(){}typenamestd::enable_if::value>::typebar(){}};错误信息:14:5:error:'typenamestd::enable_if::value>::typeFoo::bar()'cannotbeoverloaded10:5:error:with'typenamestd::enable_if::value>::typeFoo::bar()'来源cpp.sh.我以为都是typ

c++ - if/else 在 C++ 的编译时?

考虑以下代码:#include#includetemplateclassMyClass{public:MyClass():myVar{0}{;}voidtestIf(){if(isconst){myVar;}else{myVar=3;}}voidtestTernary(){(isconst)?(myVar):(myVar=3);}protected:staticconstboolisconst=std::is_const::value;TmyVar;};intmain(){MyClassx;MyClassy;x.testIf();x.testTernary();y.testIf();/