草庐IT

static_if

全部标签

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++ - 索引上的 static_assert 在编译时知道

有没有办法静态断言编译时已知的索引,否则在运行时断言?示例:templateclassFoo{T_data[Dim];public:constT&operator[](intidx)const{static_assert(idxfoo;foo[0];foo[1];foo[2];//compilererrorfor(inti=0;i1}return0;} 最佳答案 您可以简单地抛出异常或断言。它将在constexpr上下文中编译失败。这仅在可以在constexpr上下文中评估抛出条件时才有效。请注意,某些版本的gcc中有一个错误会阻止

c++ - 为什么 GCC 不允许我创建 `inline static std::stringstream` ?

我会直接去MCVE:#includestructA{inlinestaticstd::stringstreamss;};海湾合作委员会7.2和7.1refusetocompile它有以下错误:error:nomatchingfunctionforcallto'std::__cxx11::basic_stringstream::basic_stringstream()'inlinestaticstd::stringstreamss;^~Infileincludedfromblah:1:0:/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0

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

显式类型转换(C 风格转换)的 C++ 转换符号和 static_cast 的多种解释

Paragraph4of[expr.cast](在撰写本文时可用的最新C++标准草案中)描述了C样式转换的行为如下:Theconversionsperformedbyaconst_­cast,astatic_­cast,astatic_­castfollowedbyaconst_­cast,areinterpret_­cast,orareinterpret_­castfollowedbyaconst_­cast,canbeperformedusingthecastnotationofexplicittypeconversion.Thesamesemanticrestrictionsan

c++ - boost static_vector 而不是 std::is_trivially_destructible

根据thisexample(左例)#include#includestructX{intk;std::arraya;boost::container::static_vectorb;~X()=default;};inthuh(){std::arrayx;return0;}看起来像boost::container::static_vector当T时可以轻易破坏是(当b被销毁时,不会在X上循环)。huh优化为xoreax,eax;ret(即return0不遍历数组。当我改用具有非平凡析构函数的包含类型时(右例)#include#includestructY{~Y();};structX{i

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

我有一段相当复杂的代码,我将其简化为这个复制器:#include#includetemplatestructouter{templatestructinner{templatestructproblem;usingTA=std::tuple;usingTB=std::tuple;templatestructproblem::value::value>::type>{staticconstexprautoval(){return1;}//actuallyacomplexfunction};templatestructproblem::value>=std::tuple_size::val

c++ - 英特尔线程构建 block 并发队列 : Using pop() over pop_if_present()

与pop()相比,使用阻塞调用有什么区别,while(pop_if_present(...))哪个应该优先于另一个?为什么?我希望更深入地了解在while(pop_if_present(...))情况下轮询自己与让系统为您完成轮询之间的权衡。这是一个很普遍的主题。例如,使用boost::asio我可以执行myIO.run()来阻止或执行以下操作:while(1){myIO.poll()}一个可能的解释是调用while(pop_if_present(...))的线程将保持忙碌,所以这很糟糕。但是某人或某物必须轮询异步事件。当它委托(delegate)给操作系统或库时,为什么以及如何能更便

C++ 摆脱单例 : alternative to functors and static methods

我崇高的追求是摆脱单例和静态类。背景:我有以下结构:Cmd经常实例化的对象,它保存命令的名称(字符串),以及任何类的静态方法的仿函数作为指针。它通常在主类中创建,例如Input、Console、Render等,并引用创建它的类中的方法,为这些方法提供运行时口头接口(interface)。Cmds还以字符串数组的形式解释参数,其中第一个参数是Cmd的名称,所有连续的字符串都是被调用的静态方法的直接参数。参数计数和参数数组存储在Commander中,并在每次Cmd调用之前更改。CommanderCommander用于解释字符串命令(可能直接或通过控制台),并执行以字符串形式存储在缓冲区中的

c++ - if语句中的getline

根据我的阅读,在bool上下文中使用的getline()返回到void*的隐式转换。我在网络上的任何地方都没有找到对这一声明的任何真正引用。它到处都说隐式转换不存在,并且在bool上下文中指针应该是同类的(如果ptr==0比0转换为类型指针ptr).同样在标准中说在bool上下文中它被转换为未指定的bool类型。这到底是什么意思? 最佳答案 简而言之:这意味着您可以在if语句中使用getline(),如果有效,您将进入if语句block。在龙:getline()usedinaBooleancontextreturnsanimplic