草庐IT

T_OLD_FUNCTION

全部标签

c++ - std::function 和 std::mem_fn 有什么区别

我无法弄清楚两个函数包装器之间的区别std::function和std::mem_fn.从描述来看,在我看来,std::function可以完成std::mem_fn所做的一切,甚至更多。在哪种情况下会使用std::mem_fn而不是std::function? 最佳答案 您不能真正将std::function与std::mem_fn进行比较。前者是你指定类型的类模板,后者是未指定返回类型的函数模板。在任何情况下,您实际上都不会真正考虑一个与另一个。更好的比较可能是mem_fn和std::bind。在那里,对于指向成员的指针的特定用

c++ - c++ : error: must use '.*' or '->*' to call pointer-to-member function in function 中的函数指针

代码片段如下。无法理解为什么会出现此错误。voidSipObj::check_each_field(){map::iteratormsg;stringstr;charname[20];boolres=false;sscanf(get_payload(),"%s%*s",name);LOGINFO(lc())second;res=(this).*sip_field();}}typedefbool(SipObj::*sip_field_getter)();staticmapsip_field_map;sip_field_getter是函数名的占位符 最佳答案

C++ 低延迟设计 : Function Dispatch v/s CRTP for Factory implementation

作为系统设计的一部分,我们需要实现工厂模式。结合工厂模式,我们还使用CRTP来提供一组基本功能,然后可以由派生类进行自定义。示例代码如下:classFactoryInterface{public:virtualvoiddoX()=0;};//forceallderivedclassestoimplementcustom_X_impltemplateclassCRTP:publicBase{public:voiddoX(){//docommonprocessing.....thenstatic_cast(this)->custom_X_impl();}};classDerived:pub

c++ - std::function 如何知道调用约定?

int__cdeclccall(inti){wprintf(L"ccall(%d)",i);return0;}int__stdcallstdcall(inti){wprintf(L"stdcall(%d)",i);return0;}int__cdeclwmain(intargc,wchar_t**argv){std::functionfnc=ccall;std::functionfnstd=stdcall;fnc(10);//printedwellfnstd(100);//printedwellreturn0;}我担心如何将__stdcall函数分配给std::function对象。但

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

我有一些非常简单的(C++11)代码,最新的clang(version3.4trunk187493)无法编译,但GCC编译正常。代码(下面)实例化函数模板foo使用局部函数类型Bar然后尝试将其地址用作类模板Func的非类型模板参数:templatestructFunc{};templateexterninlinevoidfoo(){usingFoo=Func>;}intmain(){structBar{};//function-localtypefoo();return0;}clang发出以下错误:error:non-typetemplateargumentreferstofunct

c++ - std::function 复制参数?

我的代码:#include#includeusingnamespacestd;structA{A()=default;A(constA&){coutf=&foo;Aa;f(a);return0;}我在控制台上看到两次“copiedA”。为什么对象被复制了两次,而不是一次?我怎样才能正确地防止这种情况? 最佳答案 专业std::function有一个带有以下声明的调用运算符:Roperator()(Args...)const;在您的例子中,这意味着运算符(operator)需要A.因此,调用f(a)由于按值传递语义,结果是一个拷贝。然

c++ - g++:用闭包类型初始化的 std::function 总是使用堆分配?

Internet上的一些来源(特别是thisone)说std::function使用小闭包优化,例如如果闭包大小小于一定数量的数据,它不会分配堆(上面的链接表示gcc为16字节)所以我深入研究了g++header看起来是否应用这种优化是由“功能”header(g++4.6.3)中的这段代码决定的staticvoid_M_init_functor(_Any_data&__functor,_Functor&&__f){_M_init_functor(__functor,std::move(__f),_Local_storage());}还有几行:staticvoid_M_init_func

c++ - 通过非常量引用将使用自动关键字声明的 lambda 作为参数传递给 std::function 参数类型

考虑以下代码:#include#include//passingfunctionobjectbyvaluevoidfoo(std::functionf){std::cout&f){std::cout&f){std::coutg1=[](intx,inty){returnx*y;};autog2=[](intx,inty){returnx*y;};foo(g1);//OKfoo(g2);//OKbar(g1);//OKbar(g2);//OKfizz(g1);//OK//compileerror://error:invalidinitializationofreferenceoftype

C++ 错误 : base function is protected

我想知道为什么下面的代码不能编译:classbase{protected:typedefvoid(base::*function_type)()const;voidfunction_impl()const{}//error:‘voidbase::function_impl()const’isprotected};classderived:publicbase{public:operatorfunction_type()const{returnboolean_test()==true?&base::function_impl:0;//error:withinthiscontext}pro

c++ - C4711 "function selected for inline expansion"Visual C++ 警告有什么用?

AccordingtoMSDNVisualC++可以发出C4711警告:如果编译器决定内联未标记为内联的函数,则选择函数X进行内联扩展。我不明白这个警告有什么用。假设我编译我的代码并看到这个警告。怎么办?我为什么要关心? 最佳答案 默认情况下未启用。如果出于某种原因你想知道什么时候内联函数,你可以打开它。这可能是相关的,例如,代码大小非常重要,或者您希望从模块外部跳入函数,或者您需要程序集以某种方式显示。它还可以帮助追踪代码生成错误。这纯粹是信息性的。 关于c++-C4711"funct