草庐IT

if-constexpr

全部标签

c++ - 为什么 for-loop 不是编译时表达式并且扩展的 constexpr 允许在 constexpr 函数中进行循环

我是这样写代码的#includeusingnamespacestd;constexprintgetsum(intto){ints=0;for(inti=0;i我知道它之所以有效是因为extendedconstexpr.然而在这个问题why-isnt-a-for-loop-a-compile-time-expression,作者给出了他的代码如下:#include#include#includeconstexprautomultiple_return_values(){returnstd::make_tuple(3,3.14,"pi");}templateconstexprvoidfoo

c++ - 如何决定 constexpr 是否返回引用

如果您有一个函数,ifconstexpr()决定做一件事或另一件事,如何在一种情况下返回左值而在另一种情况下返回右值?以下示例在第一个用法行中未编译,因为返回类型auto不是引用:staticintnumber=15;templateautoget_number(intsometemporary){ifconstexpr(getref){returnnumber;//wewanttoreturnareferencehere}else{(...)//dosomecalculationswith`sometemporary`returnsometemporary;}}voiduse(){i

c++ - 哪个更快?函数调用还是条件 if 语句?

在回答这个问题之前,请考虑分支预测。在某些情况下,我可以在函数指针的帮助下将条件语句替换为对函数的调用。类似这样的事情。(对于类似类型的senario,您可以考虑基于组件的编程而不是继承)classShape{floatArea(){if(type==SQUARE){returnlength*length;}elseif(type==RECTANGLE){returnlength*breadth;}}}同一个类可以这样写。classShape{voidSetAreaFunction(void*funcptr)//thisfunctionisusedtosetthecurrentArea

c++ - 如何将 constexpr 函数的参数标记为未使用?

考虑这个经典示例:templateconstexprstd::size_tarraySize(T(&array)[N])noexcept{returnN;}现在这工作正常,但有一个烦恼,gcc给出警告:warning:unusedparameter‘array’[-Wunused-parameter]已知解决方案:不起作用:如果我将经典的(void)arr;添加到函数中,我会得到error:bodyofconstexprfunction'...'notareturn-statement。不满意:我可以使用arraySize(T(&)[N]),但我想为参数命名有两个原因:它使编译器错误消

c++ - If-else-if 与 map

假设我有这样一个if/else-if链:if(x.GetId()==1){}elseif(x.GetId()==2){}//...50moreelseifstatements我想知道的是,如果我保留一张map,它在性能方面会不会更好?(假设键是整数) 最佳答案 map(通常)是使用红黑树实现的,它提供O(logN)查找,因为树始终保持平衡。您的if语句线性列表将是O(N)最坏的情况。所以,是的,map的查找速度会快得多。许多人建议使用switch语句,这对您来说可能不会更快,具体取决于您的实际if语句。编译器有时可以通过使用O(1)

c++ - static_if 会弃用模板特化吗?

像这样的一些常用模板特化:templateclassC{voidcommon(){...}voidf2=delete;};templateclassC{voidcommon(){...}voidf1(){...}};可以用static_if表示作为:templateclassC{voidcommon(){...}static_if(std::is_same::value){voidf1(){...}}else{voidf2()=delete;}}这些是直接竞争的功能吗?模板特化可以做static_if做不到的事情吗?看起来static_if可以做模板特化可以做的一切,甚至更多。顺便说一

c++ - 为什么 GCC 在编译时不评估 constexpr?

举个例子:classsomething{public:staticconstexprintseconds(inthour,intmin,intsec){returnhour*3600+min*60+sec;}}然后:printf("Lookatthetime:%d\n",something::seconds(10,0,0));将使用g++编译为对函数的调用,而不是放置常量。为什么g++会那样做?它没有任何好处,而且有点违背了使用constexpr而不是可怕的宏的目的。 最佳答案 Whywouldg++dothat?constexpr

C++ std::set::erase 与 std::remove_if

此代码有VisualStudioerrorC3892。如果我将std::set更改为std::vector-它有效。std::seta;a.erase(std::remove_if(a.begin(),a.end(),[](intitem){returnitem==10;}),a.end());怎么了?为什么我不能将std::remove_if与std::set一起使用? 最佳答案 您不能使用std::remove_if()具有const的序列部分。std::set的序列元素由Tconst组成对象。事实上,我们昨天在标准C++委员会

c++ - 此 C/C++ if() 语句的计算结果是否为 TRUE?

根据PC-lint,以下语句永远不会TRUE:if((variable&0x02)==1)我正在为嵌入式系统使用C编译器,只要设置了variable中的相应位,它就会将其评估为TRUE。我猜编译器正在对==的两侧进行TRUE/FALSE比较,而不是比较结果数字。换句话说,每当表达式(varable&0x02)不为零(即TRUE)时,语句也将为TRUE,因为值1也是TRUE(不是零)。我不知道C/C++标准是否明确定义了编译器在这种情况下的行为方式。有没有C/C++专家可以根据标准(例如C90、C99等)回答这个问题?P.S.:在上面的语句中,“variable”是一个unsignedc

c++ - constexpr 函数的主体不是返回语句

在下面的程序中,我在func()中添加了显式的return语句,但是编译器给出了以下错误:m.cpp:Infunction‘constexprintfunc(int)’:m.cpp:11:1:error:bodyofconstexprfunction‘constexprintfunc(int)’notareturn-statement}这是代码:#includeusingnamespacestd;constexprintfunc(intx);constexprintfunc(intx){if(x我已经使用以下命令在g++编译器中编译了程序。g++-std=c++11m.cpp我在函数中