草庐IT

If-Modified-Since

全部标签

c++ - 时间与 "as-if"规则

有一个很棒的questionaboutthe"as-if"rule一般来说,但我想知道在测量时间方面是否有任何异常(exception)情况。考虑这个(取自here稍作修改):usingstd::chrono;autobegin=steady_clock::now();autoresult=some_lengthy_calculation(some_params);autoend=std::chrono::steady_clock::now();std::cout(end-begin).count()允许编译器应用任何产生相同结果的优化。这里的要点是“as-if”规则并不直接适用于测量

c++ - 如何在不重复代码的情况下使用 if-constexpr?

目前我在做:ifconstexpr(constexpr_bool_var1){autoarg1=costly_arg1();autoarg2=costly_arg2();if(costly_runtime_function(arg1,arg2)){//doX,possiblymoreconstexprconditions//doY//...}}else{//doX,possiblymoreconstexprconditions//doY//...}一种可能的方法是将doX/Y等转换为一个函数doXY()并在两个地方调用它,但是它看起来很笨拙,因为我必须编写一个函数,它只存在于方便元编程

c++ - 编译器优化开关的方式是否不同于长 if-then-else 链?

假设我有N个在编译时已知的不同整数值,V_1到V_N。考虑以下结构:constintx=foo();switch(x){caseV_1:{/*commandsforV_1whichdon'tchangex*/}break;caseV_2:{/*commandsforV_1whichdon'tchangex*/}break;/*...*/caseV_N:{/*commandsforV_1whichdon'tchangex*/}break;}对比constintx=foo();if(x==V_1){/*commandsforV_1whichdon'tchangex*/}elseif(x==

c++ - 转发声明使用 enable_if : ambiguous call 的函数

我在声明一个使用boost::enable_if的函数时遇到了一些麻烦:下面的一段代码给我一个编译器错误://Declarationtemplatevoidfoo(Tt);//Definitiontemplatetypenameboost::enable_if>::typefoo(Tt){}intmain(){foo(12);return0;}编译时,出现“对foo的模糊调用”错误。根据enable_if的定义,'type'typedef在条件为真时对应于void,据我所知,的两个签名foo匹配。为什么编译器认为它们不同,是否有正确的方法来转发声明foo(最好不要重复enable_if

c++ - 如何有效地将 if 和 else 用于过滤结构?

为了解析从JavaScript获取的函数参数,我需要执行大量检查。例如,一个函数可能需要一个对象作为参数,在JavaScript中看起来像这样。{Fullscreen:['bool',false],Size:['Vector2u',800,600],Title:['string','HelloWorld'],//moreproperties...}在C++中,我通过遍历所有键并检查它们来解析它。如果其中一项检查失败,则应打印错误消息并跳过此键值对。这就是我目前的实现方式。我希望您不会因某些特定于引擎的调用而分心。ModuleSettings*module=(ModuleSettings

c++ - enable_if 用于没有返回类型的函数

我最近遇到了一个有趣的enable_if用法版本,它用于有条件地启用具有更好可读性的函数,因为该函数的返回类型不是enable_if的一部分(请参阅cleaner_usage):#includeusingmaybe_integral=int/*=orfloat--thenwon'tcompile*/;usingreturn_type=int;typenamestd::enable_if::value,return_type>::typetraditional_usage(){return1;}template::value,int>::type=0>return_typecleaner

c++ - "If you' 已经写了一个编译测试,你've written a call to main"

在程序中调用mainviolatesC++标准voidf(){main();//anendlessloopcallingmain?Nothat'snotallowed}intmain(){staticint=0;std::cout在lecture中ChandlerCarruth,大约在“22.40”说ifyou'vewrittenacompilertestyou'vewrittenacalltomain这有什么关系,或者如何克服标准不允许的事实? 最佳答案 这里的要点是,如果你编写编译器测试代码,你可能会想用一些不同的参数集测试调用

c++ - std::remove_if 不删除所有项目

在输入中我想删除所有非唯一值。我希望删除双项后的子集与输入相同。不知何故,一些字符保留在输入中,但并非所有字符都被删除。谓词中的std::map似乎也在减小大小。我使用的std::remove_if()谓词是:templateclassRemovePredicate{public:RemovePredicate():m_oldsize(0){}booloperator()(constT&value){//boolretval;m_uniques[value]='a';//'a'couldbeanyvaluecoutm_uniques;unsignedm_oldsize;};我设计谓词的

c# - 为什么 if( !A && !B ) 不能优化为单个 TEST 指令?

if(!A&&!B)似乎应该编译为moveax,dwordptr[esp+A_offset]testeax,dwordptr[esp+B_offset]jne~~~~~~~~~~编译器实际生成moveax,dwordptr[esp+A_offset]testeax,eaxjne~~~~~~~~~~moveax,dwordptr[esp+B_offset]testeax,eaxjne~~~~~~~~~~看这里转储8B45F8moveax,dwordptr[b]837DFC00cmpdwordptr[a],07504jnemain+32h(0A71072h)85C0testeax,eax7

c++ - template<class = enable_if_t<...>> 做什么?

我一直在阅读STL文件,以学习格式化代码的更好方法,并学习提高效率的技巧。我一直在阅读线程文件,但我无法弄清楚某些代码的作用。template,thread>::value>>explicitthread(_Fn&&_Fx,_Args&&..._Ax){//constructwith_Fx(_Ax...)...}std::enable_if_t是templateusingenable_if_t=typenameenable_if::type;templatestructenable_if{//typeis_Tyfor_Testusingtype=_Ty;};该代码在thread和str