草庐IT

T_OLD_FUNCTION

全部标签

c++ - 存储在 std::function 中时,无捕获 lambda 无法转换为函数指针

通常,没有捕获的C++lambdashouldbeconvertable到c风格的函数指针。不知何故,使用std::function::target转换它不起作用(即返回一个nullptr),而且target_type与签名类型不匹配,即使它看起来一样。在VC13和GCC5.3/5.2.0/4.8上测试最小测试示例:#include#includevoidMaybe(){}voidcallMe(std::functioncallback){typedefvoid(*ftype)();std::cout()预期的输出是11实际输出001问题是:为什么lambda的签名与传递的函数不同?

c++ - 编译器错误 : Function call with parameters that may be unsafe

得到了一些不是我的代码并且它产生了这个警告atm:iehtmlwin.cpp(264):warningC4996:'std::basic_string::copy':Functioncallwithparametersthatmaybeunsafe-thiscallreliesonthecallertocheckthatthepassedvaluesarecorrect.Todisablethiswarning,use-D_SCL_SECURE_NO_WARNINGS.SeedocumentationonhowtouseVisualC++'CheckedIterators'with[_

c++ - 替代 std::function 将函数作为参数传递(回调等)

我在使用C++11进行实验时偶然发现了这一点。我发现这是一个明显的解决方案,但我无法在野外找到任何其他示例,所以我担心我遗漏了一些东西。我指的做法(在“addAsync”函数中):#include#include#include#includeintaddTwoNumbers(inta,intb){std::coutfuture){std::cout)->void){//number){//lambdafunctionsworkgreataddAsync(number.get(),20,[](std::futurenumber){addAsync(893,4387,printNum);

c++ - 使用 std::function 中函数的类型来声明该类型的多个函数

假设我有typedefstd::functionFooType;我想为一系列函数声明函数原型(prototype),我可以将它们插入到这种类型的std::function中。我知道我会写doublefoo1(int,long);doublefoo2(int,long);等等,但是有没有办法在声明函数原型(prototype)时以某种方式使用FooType?类似的东西FooType::typefoo1,foo2;也许我可能不得不使用(*foo1)或类似的?自然,在函数的实现中,我需要把它拼写出来,这样我就可以输入一些参数,但是像上面这样写可以让我的头文件更干净。

c++ - "cannot be used as a function error"

我正在编写一个使用不同.cpp文件中的函数的简单程序。我所有的原型(prototype)都包含在一个头文件中。我将一些函数传递给其他函数,但不确定我是否正确执行。我得到的错误是“'functionname'不能用作函数”。它说不能使用的函数是growthRate函数和estimatedPopulation函数。数据通过输入函数输入(我认为这是有效的)。谢谢!头文件:#ifndefheader_h#defineheader_h#include#include#includeusingnamespacestd;//prototypesvoidexterninput(int&,float&,

c++ - 类声明的编译错误 "looks like a function definition"是什么意思?

我最近遇到了这个问题。我发现很多人都在问这个问题——here,forexample——但没有具体的答案。这是从该链接中提取的示例代码:classAFX_BASE_APPLICATION_APP_CLASSCFileExtension{public:CFileExtension();virtual~CFileExtension();};这产生的错误是:c:\FileExtension.h(14):errorC2470:'CFileExtension':看起来像函数定义,但没有形参列表;跳过明显的body 最佳答案 您几乎肯定错过了定义

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