草庐IT

if-cases

全部标签

c++ - 如何在本身取决于另一个条件的条件下使用 std::enable_if?

我有一种情况需要使用std::enable_if来区分两个重载,比如foo。赋予std::enable_if的条件本身取决于foo的模板参数的依赖类型。使用std::enable_if表达这一点的最佳方式是什么?以下测试代码是我目前所拥有的。我意识到除了std::enable_if之外可能还有更好的方法来实现我在测试代码中想要的行为。但是,以下是我的用例的简化版本,它本身需要std::enable_if。#include#includestructbar{usingbaz=int;};templatestructis_bar:std::false_type{};templatestru

c++ - 使用 std::enable_if 的正确方法

这些类之间有什么区别?正是这些带有enable_if的方法。///Aliasofstd::enable_if...templateusingEnable_if=typenamestd::enable_if::type;TemplateclassA{...template>Some_Return_Typemethod(param1,param2){}template>Some_Return_Typemethod(param1){}};TemplateclassB{...Enable_ifmethod(param1,param2){}Enable_ifmethod(param1){}};在

c++ - 为什么 boost::enable_if 不会导致重复重载方法编译错误

我已经获得了enable_if的代码,它允许我做一些很棒的事情,但我认为它会导致错误,因为我下面显示的两个方法具有相同的方法签名。有人知道为什么允许这样做吗?#include#includetemplatestructenable_if{typedefRtype;};templatestructenable_if{};templatetypenameenable_if::value>::typeprint(constT&item){std::couttypenameenable_if::value)>::typeprint(constT&item){std::cout

c++ - Barton-Nackman 与 std::enable_if

什么是更可取的(如果有的话)?变体A(Barton-Nackman):templatestructequal_comparable{friendbooloperator==(constT&t1,constT&t2){returnt1.equalTo(t2);}};classMyClass:privateequal_comparable{boolequalTo(constMyClass&other)//...};变体B(std::enable_if):structMyClass{staticconstbooluse_my_equal=true;boolequalTo(constMyCla

c++ - STL find_if 和不区分大小写的字符串比较

我有一个Models的vector,如下所示:structModel{std::stringmName;//.......};给定一个表示模型名称的字符串,我想看看是否可以在vector中找到其中一个模型。现在我有这个:std::stringassetName="monkey";std::vector::iteratoriter=std::find_if(mModels.begin(),mModels.end(),boost::bind(&Model::mName,_1)==assetName);但是这不会进行不区分大小写的字符串比较。所以我阅读了有关boost/algorithm/s

C++ copy_if lambda 捕获 std::string

这是来自此处的后续问题:C++-Developingownversionofstd::count_if?我有以下功能://vectorforstoringthefilenamesthatcontainssoundstd::vectorFilesContainingSound;voidContainsSound(conststd::unique_ptr&s){//OpentheWavfileWavwaveFile=Wav("Samples/"+s->filename_);//Copythesignalthatcontainsthesufficientenergystd::copy_if(

c++ - enable_if 不能用于禁用此声明

我显然没有足够的SFINAE经验来处理这个问题。实际上,我的印象是它一直有效到现在,并且在我的代码中到处都出现了这种问题,就像最近半个小时一样。#includeusingnamespacestd;template=100>::type>structmore_than_99{};intmain(){more_than_99c;}它说Notypenamed'type'in'std::__1::enable_if';'enable_if'cannotbeusedtodisablethisdeclaration在与模板声明对应的行上。到底是怎么回事?我一直使用这种语法来启用和禁用我的模板类,它

c++ - if constexpr inside lambda, 不同的编译器行为

考虑以下代码。如果我对ifconstexpr的理解是正确的,则不应编译else分支,因此不应将z()视为错误。#includestructZ{};templatevoidf(Tz){autolam=[z](){ifconstexpr(std::is_same::value){}else{z();}};}intmain(){f(Z{});}在clang和gcc这编译;但对于最新的MSVC,它没有。不幸的是,goldbolt的MSVC太旧了,但在我的机器上完全更新了VS2017,cl/std:c++17产生了:Microsoft(R)C/C++OptimizingCompilerVersi

c++ - 使用嵌套类的奇怪 enable_if 行为(MSVC 编译器错误或功能?)

在调试我的代码一段时间后,我使用enable_if追踪到我的问题的原因是一些意外的模板特化结果:以下代码在VisualStudio2010(和2008)中的DoTest()中断言失败,而在g++3.4.5中则没有。但是,当我从SomeClass中删除模板或将my_condition移出SomeClass的范围时,它也适用于MSVC。此代码是否有问题可以解释此行为(至少部分),或者这是MSVC编译器中的错误?(使用这个示例代码,boost和c++0xSTL版本是一样的)#include#includetemplateclassSomeClass{public:templatestruct

c++ - 用短路操作替换链式 ifs

这是一个非常微不足道的问题:有四个bool函数:a()、b()、c()和d()。我想继续按顺序调用它们,直到第一个返回true。而不是做传统的if(!a()){if(!b()){if(!c()){d();}}}或if(!a()&&!b()&&!c())d();我考虑将表达式写成短路求值。(a()||b()||c()||d());但我从未见过在C/C++代码中以这种方式完成此测试。我遗漏的这种方法有什么问题吗?谢谢。 最佳答案 您编写的代码是有效的。d()只有在其他bool函数返回false时才会被评估。然而,短路计算的可读性较差,并