草庐IT

static_if

全部标签

c++ - 在类定义之外定义 static const

我们是否应该在类定义之外定义一个staticconst成员,即使它是在类内部初始化的?#includeusingnamespacestd;classabc{staticconstintperiod=5;intarr[period];public:voiddisplay(){cout注释完//constintabc::period;后,两个版本的代码在gcc4.3.4上运行良好。所以我想问一下为什么这两个版本都有效,哪个版本符合标准? 最佳答案 您通过编写constintabc::period;来定义静态成员period。您可以为类的

c++ - dxvahd.h微软头文件中的#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)什么时候变为true

您好,我有2个VC++解决方案“A”和“B”(VS2008),它们都具有相同的代码库(只有几行代码不同)。在两者中使用DXVAHD.h。dxvahd.h是标准的Microsoft头文件。如果我们打开这个头文件,我们会看到有一个条件if“#ifWINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)”IseethatinVC++solution"A",theaboveconditional#ifstatementisfalse,hencethewholedxvahdheaderfilegetsgreyedout&isnotevencompiled

c++ - 为什么编译器说 : 'enable_if' cannot be used to disable this declaration

templateusingEnable_if=typenamestd::enable_if::type;classDegree;templateconstexprinlineboolIs_Degree(){returnstd::is_base_of::value;}classDegree{public:std::size_tinDeg=0;};templateclassVertex:publicSatellite{public:explicitVertex(intnum):n(num){}private:std::size_tn;};templateclassEdge{public:/

c++ - 使用 enable_if 专门化模板方法

我正在编写一个模板类,它存储一个std::function以便稍后调用它。这是简化的代码:templatestructTest{voidcall(Ttype){function(type);}std::functionfunction;};问题是这个模板不能为void类型编译,因为voidcall(voidtype)变得未定义。将它专门用于void类型并不能缓解问题,因为templatevoidTest::call(void){function();}仍然与call(TType)的声明不兼容。因此,利用C++11的新特性,我尝试了std::enable_if:typenamestd::

c++ - 我怎样才能使 std::find_if 和 std::map 使用一些 boost 库协同工作?

这个问题的灵感来自anothertopic这提出了这个问题:Findthefirstvaluegreaterthanuserspecifiedvaluefromamapcontainer可以通过多种方式解决。典型的C++03解决方案定义了一个专用函数(或仿函数)并将其传递给std::find_if作为第三个参数。在C++11中,可以避免定义专用函数(或仿函数),而是可以使用lambda作为:autoit=std::find_if(m.begin(),mp.end(),[n](conststd::pair&x)->bool{returnx.second>n;});这是theaccepte

c++ - 为什么我们应该只在类或函数 (C++) 中使用 'static'?

我最近在阅读StroustrupsTheC++ProgrammingLanguage,在第9章有关Linkage的部分中,我看到了以下段落:"InCandolderC++programs,thekeywordstaticis(confusingly)usedtomean"useinternallinkage".Don'tusestaticexceptinsidefunctionsandclasses."问题是进一步阅读,作者没有详细说明为什么这是不好的做法。我不时在我的代码中使用静态函数,通常是为了一些在编译单元之外不需要的简单计算,但我从来没有意识到这是不受欢迎的,而且我也不清楚为什

c++ - static_cast 如何将 int 转换为 char 而不是 reinterpret_cast?

我不确定之前是否有人问过它,但我相信一定是。考虑问题开头的简单行:inta;charb=reinterpret_cast(a);我明白了reinterpret_cast将类型x的位模式解释为类型y,当然,由于大小不匹配,它不应该工作,事实上它不工作。现在考虑另一个代码:inta;charb=static_cast(a);这行得通!.现在我的问题是它是如何工作的?我的意思是编译器会砍掉这些位吗?.我确定sizeof(char).如果是,reinterpret_cast也应该通过相同的技术工作吗? 最佳答案 从int到char有一个明确

c++ - 我应该使用它还是 static_cast<void*> 然后 static_cast<myType*> 来避免重新解释?

我看到有人建议使用static_cast(static_cast(p))而不是重新解释类型转换。我不明白为什么这样更好,谁能解释一下?为了便于讨论,这里有一个需要reinterpret_cast的示例场景:DWORDlpNumberOfBytes;ULONG_PTRlpCompletionKey;LPOVERLAPPEDlpOverlapped;GetQueuedCompletionStatus(myHandle,&lpNumberOfBytes,&lpCompletionKey,&lpOverlapped,0);if(lpCompletionKey==myCustomHandlerK

c++ - if 语句中的函数名以一种奇怪的方式转换

使用此代码(有效的C++11):#include#includeboolmy_awesome_func(intparam){return(param>1);}intmain(intargc,charconst*argv[]){fprintf(stderr,"typeofmy_awesome_func:%s\n",typeid(my_awesome_func).name());if(my_awesome_func){fprintf(stderr,"WHAT???\n");}return0;}问题在if语句中。虽然typeid返回一些看起来像FbiE的东西(我认为这是函数类型的gcc语言)

c++ - -static-libstdc++ 适用于 g++ 但不适用于纯 gcc?

作为引用,我使用的是MinGW(GCC5.3)。使用编译文件时g++file.cc-static-libstdc++它静态链接C++标准库(libstdc++)并生成1.9MB的可执行文件。无论如何运行gcc-lstdc++-static-libstdc++file.cc它仍然动态链接到libstdc++-6.dll并生成一个34KB的可执行文件。为什么-static-libstdc++仅适用于g++而不适用于纯gcc? 最佳答案 GCCmanual,LinkOptions说:-static-libstdc++Whentheg++p