草庐IT

enable_if_c

全部标签

c++ - 为什么 "if constexpr"不能用 Visual Studio 2017 15.3 编译?

C++17引入的ifconstexpr语法应该与/std:c++14编译器开关一起使用,根据此文档:C++17FeaturesInVisualStudio2017Version15.3Preview.但是,它不起作用。相反,会生成以下编译器错误:errorC4984:'ifconstexpr'isaC++17languageextension文档有错吗?如果是这样,ifconstexpr如何在VisualStudio201715.3中编译? 最佳答案 看起来问题中链接的文档在这里不准确。要在VisualStudio2017中使用if

C++ 使用带字符串的标准算法,带 isdigit 的 count_if,函数转换

我想以最短的代码方式计算字符串中的所有数字。我这样试过:#include#includeunsignedcountNumbers(conststd::strings){returncount_if(s.begin(),s.end(),isdigit);}错误信息是:a.cc:Infunction‘unsignedintcountNumbers(std::string)’:a.cc:5:45:error:nomatchingfunctionforcallto‘count_if(std::basic_string::const_iterator,std::basic_string::con

c++ - 使用 std::enable_if 时的对象切片

我正在尝试使用std::enable_if来专门化一个类,如果它的一个子类定义了特定的成员函数。否则,它应该使用在基类中定义的默认实现。#include#include#include#include#include#includeBOOST_TTI_HAS_MEMBER_FUNCTION(f2)classBase{public:virtualdoublef1(doublex,doubley)const{std::coutclassA:publicBase{public:templatetypenamestd::enable_if,boost::function_types::cons

c++ - "if constexpr"与 "try in constexpr function"警告交互

我声称thisprogram应该是合式的:它声明了S的constexpr成员函数.但是,GCC和Clang都拒绝这个程序。templatestructS{constexprintfoo(){ifconstexpr(std::is_same_v){return0;}else{try{}catch(...){}return1;}}};intmain(){Ss;returns.foo();//expect"return0"}海湾合作委员会说:error:'try'in'constexpr'functionclang说:error:statementnotallowedinconstexprf

kotlin - 为什么编译器认为这个 if 语句是一个表达式?

我有这种方法可以比较已排序的列表并告诉您列表2中缺少列表1中的哪些项目,反之亦然,在O(N)时间内:fun>compareSortedLists(list1:Iterable,list2:Iterable,onlyInList1:MutableCollection,onlyInList2:MutableCollection){valit1=PeekingIterator(list1.iterator())valit2=PeekingIterator(list2.iterator())while(it1.hasNext()&&it2.hasNext()){valcomp=it1.peek

kotlin - 为什么编译器认为这个 if 语句是一个表达式?

我有这种方法可以比较已排序的列表并告诉您列表2中缺少列表1中的哪些项目,反之亦然,在O(N)时间内:fun>compareSortedLists(list1:Iterable,list2:Iterable,onlyInList1:MutableCollection,onlyInList2:MutableCollection){valit1=PeekingIterator(list1.iterator())valit2=PeekingIterator(list2.iterator())while(it1.hasNext()&&it2.hasNext()){valcomp=it1.peek

c++ - 嵌套的 if 语句和 "&&"运算符

if(a()&&b()&&c()&&d())doSomething();if(a())if(b())if(c())if(d())doSomething();这两者之间有“任何”性能差异吗?比如a()变为0的情况下,是否会在第一个if语句中继续运行b()、c()和d()?或者它会像第二个嵌套的if语句一样工作吗? 最佳答案 它们完全相同。要自己测试,运行gcc-Stest.c(假设这是您放置源代码的地方)并观察test.s的内容.下面是嵌套-if方法在gcc4.8.1中使用默认选项编译的方式(注释有注释):main:.LFB0:.cf

c++ - 如何在模板折叠表达式中使用 if constexpr?

我想编写一个参数数量可变的求和函数,条件是它应该忽略不是std::is_arithmetic的参数我找到了一个可行的递归版本autoold_sum(){return0;}templateautoold_sum(T1s,T...ts){ifconstexpr(std::is_arithmetic_v)returns+old_sum(ts...);elsereturnold_sum(ts...);}我想知道我是否可以在折叠表达式的上下文中使用ifconstexpr来使以下代码仅考虑参数包中的算术类型:templateautofold_sum(T...s){return(...+s);}

c++ - 在 std::remove_if 执行期间遍历容器是否安全?

假设我想从std::vector中删除unique元素(不是去除重复项,而是只保留至少出现2次的元素)并且我想以一种非常低效的方式实现这一点-通过调用std::count而std::remove_ifing。考虑以下代码:#include#include#includeintmain(){std::vectorvec={1,2,6,3,6,2,7,4,4,5,6};autoto_remove=std::remove_if(vec.begin(),vec.end(),[&vec](intn){returnstd::count(vec.begin(),vec.end(),n)==1;});

c++ - [C++ 编译时断言] : Can we throw a compilation error if some condition is not met?

我写了一个函数:templatevoidtryHarder(){for(inti=0;i但我只希望它在N介于0和10之间时编译。我可以这样做吗?怎么办? 最佳答案 您可以使用static_assertdeclaration来完成:templatevoidtryHarder(){static_assert(N>=0&&N此功能仅在C++11之后可用。如果您坚持使用C++03,请查看Boost'sstaticassertmacro.整个想法都是很好的错误信息。如果您不关心这些,或者甚至负担不起boost,您可以执行以下操作:templa