草庐IT

c++ - 为什么不能将 mem_fn 应用于 std::string 的成员函数?

structint_holder{intvalue;inttriple(){returnvalue*3;}};intmain(intargc,constchar*argv[]){std::stringabc{"abc"};int_holderone{1};autof1=mem_fn(&std::string::clear);autof2=mem_fn(&int_holder::triple);f1(abc);f2(one);}我在Xcode中测试这样的代码,编译器发出这样的错误似乎mem_fn可以用于用户定义类的成员函数,但不能用于标准字符串的成员函数,有什么不同,为什么?感谢您的阅读

C++11:std::mem_fn 的类型名称

auto很好,但我需要在类中声明一个成员,而不是堆栈中的变量。decltype有效,但不知何故看起来很奇怪classAutomation{void_init_state(int);decltype(std::mem_fn(&Automation::_init_state))next_state;};std::function似乎也可以,但与纯成员函数有细微差别classAutomation{void_init_state(int){}public:decltype(std::mem_fn(&Automation::_init_state))next_state;std::functio

C++ ptr_fun 找不到 void 参数化函数

我尝试使用std::ptr_fun来包装我的函数,但是当我尝试使用void参数和bool返回类型来包装一个函数时,我得到了一个错误:代码:std::functioncr=std::not1(std::ptr_fun(&funct1));功能:boolfunct1(){returnfalse;}错误:error:nomatchingfunctionforcallto'ptr_fun(bool(*)())'但每当我将参数更改为int时,问题似乎就消失了。如何使用void参数包装函数? 最佳答案 std::ptr_fun仅适用于一元函数:

c++ - tr1::mem_fn 和 tr1::bind:常量正确性和重载

以下代码片段有什么问题?#include#include#includeusingnamespacestd::tr1::placeholders;structabc{typedefvoidresult_type;voidhello(int){std::cout尝试用g++-4.3编译它,似乎cv-qualifier重载函数混淆了tr1::mem_fn和tr1::bind并出现以下错误:nomatchingfunctionforcallto‘bind(,...下面的代码片段编译但似乎破坏了const-correctness:structabc{typedefvoidresult_type

c++ - mem_fun_ref 麻烦

我无法理解mem_fun_ref。我必须承认,我通常将仿函数用于此类事情,因为它们可以内联以提高速度和利润。但是,这段代码不会成为瓶颈,所以我想尝试一下。这是我想做的一个例子。我知道还有其他方法可以做到这一点。我不想使用copy,我不想使用范围成员函数,我不想使用back_inserter。我特别想使用mem_fun_ref。这只是一个简单的例子,实际情况要复杂得多。也就是说,我真的不知道为什么这是错误的,但我不熟悉mem_fun_ref或mem_fun。这是我想要的工作:#include#include#include#includeusingnamespacestd;intmain

c++ - 功能性、bind1st 和 mem_fun

为什么不能编译?#include#includeclassA{A(){typedefboost::functionFunctionCall;FunctionCallf=std::bind1st(std::mem_fun(&A::process),this);}voidprocess(){}};错误:Infileincludedfrom/opt/local/include/gcc44/c++/bits/stl_function.h:712,from/opt/local/include/gcc44/c++/functional:50,froma.cc:1:/opt/local/includ

c++ - fun(...) 和 (*fun)(...) 在 C/C++ 中使用函数指针有什么区别

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Howcomepointertoafunctionbecalledwithoutdereferencing?Howdoesdereferencingofafunctionpointerhappen?假设我有一个像这样的函数指针:voidfun(){/*...*/};typedefvoid(*func_t)();func_tfp=fun;然后我可以通过以下方式调用它:fp();或(*fp)();有什么区别/

c++ - tr1::mem_fn 和具有默认参数的成员

我有一个带有默认参数的成员函数的类。structClass{voidmember(intn=0){}};通过std::tr1::mem_fn我可以调用它:Classobject;std::tr1::mem_fn(&Class::member)(object,10);就是说,如果我想用默认参数调用对象上的callable成员,正确的语法是什么?std::tr1::mem_fn(&Class::member)(object);//Thisdoesnotworkg++报错如下:test.cc:17:error:nomatchforcallto‘(std::tr1::_Mem_fn)(Clas

c++ - Clang:将 bind 或 mem_fn 与 string::c_str 和 transform 结合使用时出现问题

尝试将std::stringvector转换为constchar*vector:#include#include#include#includeintmain(intargc,char**argv){std::vectorvalues;values.push_back("test1");values.push_back("test2");values.push_back("test3");std::vectorc_values(values.size());std::transform(values.begin(),values.end(),c_values.begin(),std::

c++ - 带有 ref_qualified 成员函数的 std::mem_fn

有什么方法可以通过std::mem_fn使用ref限定的成员函数?下面的代码编译失败:classDeadPool{public:voidjump()&{std::cout错误信息:mem_fn_ex.cc:18:15:error:nomatchingfunctionforcallto'mem_fn'autocobj=std::mem_fn(&DeadPool::jump);//Won'tcompile^~~~~~~~~~~/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/functional:1233:1:not