草庐IT

member-functions

全部标签

c++ - 哪个更合适 : getters and setters or functions?

如果替代函数名称使API更明显,是否应该放弃getter和setter的“getMyValue()”和“setMyValue()”模式?例如,假设我在C++中有这个类:publicclassSomeClass{private:boolmIsVisible;public:voiddraw();voiderase();}我可以像这样添加函数来获取/设置“mIsVisible”:boolgetVisible(){returnmIsVisible;};voidsetVisible(boolvisible){if(!mIsVisible&&visible){draw();}elseif(mIsV

c++ - 在 std::function 上强制执行 "noexcept"?

此代码编译并运行,抛出int:#includevoidr(std::functionf){f();}voidfoo(){throw1;}intmain(){r(foo);}但是我希望编译器拒绝r(foo);行,因为r应该只传递一个noexcept函数。noexcept说明符似乎被忽略了。有什么办法可以实现吗?编辑:这个问题不同于Isknowledgeaboutnoexcept-nesssupposedtobeforwardedwhenpassingaroundafunctionpointer?因为我要求补救措施,特别是在std::function的情况下。

c++ - 为什么 std::function 构造函数或赋值之间存在差异?

std::function类型删除构造函数定义为:templatefunction(Ff);赋值运算符定义为:templatefunction&operator=(F&&f);(来源cppreference)为什么构造函数通过值获取f,而operator=通过转发引用获取f? 最佳答案 我只能猜测,但我猜这是因为它被添加到C++而右值引用和转发引用被添加到语言中。因此其API的某些部分获得了转发引用,而另一些则没有。有一个小优点:如果F的复制构造函数可以扔而移动不能,std::function(F)可以保证不抛出,而std::fun

c++ - 带有 C++ 模板的虚假 "use of local variable with automatic storage from containing function"?

以下代码无法在g++7.2.0中编译templateclassRequest{intcontent=0;public:friendvoidsetContent(inti,void*voidptr){Request*ptr=(Request*)voidptr;ptr->content=i;}intgetContent(){returncontent;}};intmain(){Requestreq;setContent(4,&req);returnreq.getContent();}有错误test.cpp:Ininstantiationof‘voidsetContent(int,void*

c++ - 为什么 Member Detector fallback 必须是 int?

我以为我正在理解这个类(从这里https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector):templateclassDetectX{structFallback{intX;};//addmembername"X"structDerived:T,Fallback{};templatestructCheck;typedefcharArrayOfOne[1];//typedefforanarrayofsizeone.typedefcharArrayOfTwo[2];//typedefforanarrayofsizetw

c++ - 在源文件中放置模板成员函数的特化定义(没有默认主体)是否安全?

这就是我的意思://test.hclasscls{public:templatevoidf(Tt);};--//test.cpptemplatevoidcls::f(constchar*){}--//main.cppintmain(){clsc;doublex=.0;c.f(x);//givesEXPECTEDundefinedreference(linkererror)constchar*asd="ads";c.f(asd);//worksasexpected,NOerrorsreturn0;}这是完全可以的,对吧?我开始对此表示怀疑,因为我刚刚遇到了specializationof

c++ - 引用限定的成员函数作为模板参数?

这在clang3.3中编译得很好:templatestructM;templatestructM{};templatestructM{};但在gcc4.8.1中失败:[...]error:redefinitionof‘structM’structM{};^[...]error:previousdefinitionof‘structM’structM{};^当在不同的上下文中使用时,这会导致各种意外的编译器行为,例如崩溃或内部编译器错误。我知道ref限定的成员函数在标准中被称为“*this的右值引用”(N2439),并且是supportedbygcc4.8.1.这里的问题是将它们用作模板

C++:莫名其妙的 "pure virtual function call"错误

我在使用MicrosoftVisualC++2015时遇到了一些困难,但能够用一个小程序重现该问题。给定以下类:classBaseClass{public:BaseClass():mValue(0),mDirty(true){}virtual~BaseClass(){}virtualintgetValue()const{if(mDirty)updateValue();returnmValue;}protected:virtualvoidupdateValue()const=0;mutableboolmDirty;mutableintmValue;};classDerivedClass:

c++ - "function"类型的优点是什么(不是 "pointer to function")

阅读C++标准,我看到有“函数”类型和“函数指针”类型:typedefintfunc(int);//functiontypedefint(*pfunc)(int);//pointertofunctiontypedeffunc*pfunc;//sameasabove我从未见过在示例之外使用的函数类型(或者我可能没有意识到它们的用法?)。一些例子:funcincrease,decrease;//declarestwofunctionsintincrease(int),decrease(int);//sameasaboveintincrease(intx){returnx+1;}//cann

c++ - 成员(member)检测代码说明

这不是整个概念,而是它用来确定类是否具有n数据成员的方法之一。这是完整的代码;SFINAE用于成员检测的普通用法。templatestructhas_X{structFallback{intX;};structDerived:T,Fallback{};templatestructS;templatestaticchar(&f(S*))[1];templatestaticchar(&f(...))[2];public:conststaticboolvalue=sizeof(f(0))==2;};Derived继承自Fallback和T的部分让我感到困惑,因为当我们重载f,&C::X是&D