草庐IT

static_if

全部标签

c++ - 带有返回 const 引用的隐式转换运算符的类的 static_cast<> 行为

我有以下类(class)(精简后只包含相关部分):#includeclassText{private:std::string_text;public:Text(std::string&&text):_text(std::move(text)){}operatorconststd::string&()const{return_text;}};我的问题是:如果我想获得一个conststd::string&,我可以这样做而不会受到任何惩罚吗:Texttext("fred");auto&s=static_cast(text);或者这会构造一个我最终得到引用的中间std::string吗?这种情

c++ - 在 if-else if 链中使用 Likely()/Unlikely() 预处理器宏

如果我有:#definelikely(x)__builtin_expect((x),1)#defineunlikely(x)__builtin_expect((x),0)if(A)returntrue;elseif(B)returnfalse;...elseif(Z)returntrue;else//thiswillneverreallyhappen!!!!raiseError();returnfalse;我能否像elseif(likely(Z))一样将likely()放在最后一个条件检查周围,以表示在编译器不影响分支预测的情况下最终语句(else)的可能性很小之前的检查?基本上,如果

c++ - 是否可以将 'enable_if' 和 'is_same' 与可变函数模板一起使用?

这两个非可变函数模板编译:templatetypenamestd::enable_if::value,void>::typetestFunction(Ta,Ub){std::couttypenamestd::enable_if::value,void>::typetestFunction(Ta,Ub){std::cout但是,类似的可变参数模板无法编译:templatetypenamestd::enable_if::value,void>::typetestFunction(Ta,U...bs){std::couttypenamestd::enable_if::value,void>:

c++ - 解决 "only static const integral data members can be initialized within a class"编译错误

以下创建全局对象会导致编译错误。#include"stdafx.h"#includeusingnamespaceSystem;usingnamespacestd;#pragmahdrstopclassTester;voidinput();classTester{staticintnumber=5;public:Tester(){};~Tester(){};voidsetNumber(intnewNumber){number=newNumber;}intgetNumber(){returnnumber;}}TestertesterObject;voidmain(void){cout>ne

c++ - 如何在转换运算符中使用 std::enable_if?

基本上我希望我的范围类型可以从Range隐式转换至Range.std::enable_if似乎是不可能的,因为该函数不带任何参数并且没有返回值。解决办法是什么?这基本上是我尝试过的:templateclassRange{T*begin_;T*end_;public:Range(T*begin,T*end):begin_{begin},end_{end}{}templateRange(T(&a)[N]):begin_{static_cast(&a[0])},end_{static_cast(&a[N-1])}{}T*Begin(){returnbegin_;}T*End(){return

c++ - 如何根据模板类型使用 std::enable_if 来启用或禁用构造函数?

我有以下模板化对象:templatestructresult{//Iwanttoenablethesetwoconstructorsonlyiftype_1!=type_2result(type_1f):foo{f}{}result(type_2b):bar{b}{}//Iwanttoenablethisconstructoronlyiftype_1==type_2result(type_1f,type_2b):foo{f},bar{b}{}//Othermemberfunctionsremoved.type_1foo;type_2bar;};如何使用std::enable_if根据需

c++ - 如何实现类似 std::copy_if 但在插入到不同容器之前应用函数的方法

完全公开,这可能是一个锤子和钉子的情况,在不需要的时候尝试使用STL算法。我在我正在使用的一些C++14代码中看到了一个重新出现的模式。我们有一个迭代的容器,如果当前元素符合某些条件,那么我们将其中一个元素字段复制到另一个容器。模式是这样的:for(autoit=std::begin(foo);it!=std::end(foo);++it){autox=it->Some_member;//Note,thecheckusuallyusesthefieldwouldaddtothenewcontainer.if(f(x)&&g(x)){bar.emplace_back(x);}}这个想法几

c++ - 是否编译了带有 if(this==NULL) 测试的类函数?

我在我们的实验中看到了这段代码片段,它实际上是在MSVC2008和G++中编译的。voidLinkList::Insert(Tn){if(this==NULL)//somecodehere}据我所知,this不能为null,因为如果未实例化,则不能在C++中调用类函数。这是一个有效的代码吗?如果是这样,背后的原因是什么?它可以用在什么地方? 最佳答案 sinceyoucannotcallaclassfunctionsinc++ifitwasn'tinstantiated问题是,你可以,但它leadstoundefinedbehavi

c++ - static_cast 可以将非空指针转换为空指针吗?

我需要为回调函数编写代码(它将在ATL中调用,但这并不重要):HRESULTcallback(void*myObjectVoid){if(myObjectVoid==0){returnE_POINTER;}CMyClass*myObject=static_cast(myObjectVoid);returnmyObject->CallMethod();}这里的void*保证是指向CMyClass的指针,所以static_cast是合法的。我关心的是代码必须尽可能可移植(至少对于较新版本的VisualC++)。所以super偏执狂我也倾向于检查CMyClass*指针-我的意思是如果结果为空

c++ - 类内部和外部的 static 关键字

static关键字一般是跟内部链接有关,但是在类内部使用的static关键字是有外部链接的吧?下面的变量m、n可以在类文件之外访问。classc{inti;intj;staticintm;staticintn;public:voidzap();staticvoidclear();}; 最佳答案 没错。关键字static被严重重载,具有太多不同的含义:在命名空间范围内的变量或函数上,它给出名称内部链接。在类成员上,它使其成为静态成员,这不会影响链接。在函数范围内的变量上,它赋予变量“静态存储持续时间”,而不是“自动”或“动态”存储持续