草庐IT

MYLIB_FUNCTION_ATTRIBUTE

全部标签

c++ - 如何使用 std::function 指向函数模板

#includeintfunc(intx,inty){returnx+y;}intmain(){typedefstd::functionFuncp;Funcpfuncp=func;return0;}但是可以指向模板函数吗?#includetemplateTfunc(Tx,Ty){returnx+y;}intmain(){typedefstd::functionFuncp;Funcpfuncp=func;return0;} 最佳答案 C++中没有模板函数这样的东西——人们随便提到的“模板函数”实际上是函数模板:定义函数的模板。因此,上

c++ - 如何在 Visual Studio 11 中将成员函数直接绑定(bind)到 std::function?

我可以轻松地将成员函数绑定(bind)到std::function,方法是使用带有捕获子句的lambda表达式包装它们。classClass{Class(){Register([=](intn){Function(n);});}voidRegister(std::functionCallback){}voidFunction(intNumber){}};但我想直接绑定(bind)它们,如下所示。//...Register(&Class::Function);//...我觉得按照C++11的标准,这个应该是支持的。但是,在VisualStudio11中,我得到了这些编译器错误。error

c++ - `std::function` 是否允许移动其参数?

在处理thisquestion时,我注意到GCC(v4.7)的std::function实现在参数被取值时会移动它的参数。以下代码显示了这种行为:#include#includestructCopyableMovable{CopyableMovable(){std::coutbyValue;byValuefooByValue=foo;CopyableMovablecm;fooByValue(cm);}//outputs:defaultcopymovemove我们在这里看到执行了cm的拷贝(这似乎是合理的,因为byValue的参数是按值获取的),但随后有两个Action。由于functi

c++ - 递归 typedef 函数定义:std::function 返回自己的类型

我正在尝试实现状态机。状态由callback_t类型的函数表示:callback_t(int&)返回相同类型的函数。我不知道如何实现它,因为似乎不允许递归类型函数。这是我尝试过的(作为玩具):#include#includetypedefstd::functioncallback_t;callback_tf1(int&i){i++;returnf1;}callback_tf0(int&i){if(i==0)i++;returnf1;}callback_tstart(int&i){i=0;returnf0;}intmain(intargc,char**argv){callback_tbe

c++ - 为什么我不能将 [](auto&&...){} 转换为 std::function<void()>?

当我尝试编译时:#includevoidf(std::functionf){}voidg(){f([](auto&&...){});}在gcc7.3上,我收到以下错误:[x86-64gcc7.3#1]error:couldnotconvert'g()::{}'from'g()::'to'std::function'有人可以解释为什么这是无效的c++吗?还是我应该提交错误报告?(MSVC14接受并将其编译为我所期望的。) 最佳答案 这是gccbug.它将您的lambda解释如下:[](auto&&,...){}所以有一个参数,然后是C

c++ - 为什么 std::function 不能绑定(bind)到 C 风格的可变参数函数?

比如这个编译失败:std::functionmy_printf(printf);使用gcc,错误信息如下:error:variable'std::functionmy_printf'hasinitializerbutincompletetypestd::functionmy_printf(printf);起初我认为这是gcc中的一个错误,但后来我查看了标准,似乎不支持这个。这是什么技术原因? 最佳答案 问题是实现之一。假设这是可能的。然后std::function必须声明(在printf的情况下)intoperator()(char

c++ - 为什么 std::function::argument_type 已被弃用?

我在cppreference上见过std::function::argument_type在C++17中已被弃用。背后的原因是什么?什么ISOWG21论文提出了这个建议? 最佳答案 相关论文是P0005R4(这是被投票纳入标准草案的论文)和P0090R0(由P0005R4引用)。来自P0090R0的报价:Q2.What'swrongwithresult_type,etc.?A2.TheseC++98/03/TR1-eratypedefspredateddecltypeandperfectforwarding.Previously,g

c++ - 智能感知 : the object has type qualifiers that are not compatible with the member function

我有一个名为Person的类:classPerson{stringname;longscore;public:Person(stringname="",longscore=0);voidsetName(stringname);voidsetScore(longscore);stringgetName();longgetScore();};在另一个类(class),我有这个方法:voidprint()const{for(inti=0;i这是人的声明:staticconstintsize=8;Personpeople[size];当我尝试编译它时,我得到了这个错误:IntelliSense

c++ - 如何正确声明以函数类型为参数的模板(如 std::function)

我发现拥有像这样的简洁语法并非易事:std::function如果我将函数声明为:templateclassfunction{};定义函数的模板类型是一种普通的语法:functionf;但它使用部分模板特化的奇怪技巧templateclassfunction;//#1templateclassfunction{}//#2这是为什么呢?为什么我需要给模板一个带有空类型参数(#1)的空通用表单,否则它就无法编译 最佳答案 这正是语言的设计方式。主模板不能像那样对类型进行复杂的分解;您需要使用部分特化。如果我理解正确,您只想编写第二个版本

c++ - std::绑定(bind)到 std::function?

我使用这个得到一个编译错误:std::vector>functions;std::functionfoo=[](inta,intb){returna+b;};std::functionbar=std::bind(foo,2);functions.push_back(bar);错误是:/usr/include/c++/4.6/functional:1764:40:error:nomatchforcallto'(std::_Bind(int)>)(int)'谁能告诉我如何将std::bind转换为std::function? 最佳答案