草庐IT

MYLIB_FUNCTION_ATTRIBUTE

全部标签

c++ - 我可以在 std::function 模板签名类型参数中使用命名参数吗?

我可以合法地在std::function(或其他类似结构)中使用模板参数名称吗?例如。给定代码std::functionsome_func;我可以用下面的方式重写吗?std::functionsome_func;如果它符合标准,那就太好了,因为类型本身很少包含有关参数用途的信息。到目前为止,我已经在VisualStudio2013中对其进行了测试,并且可以正常工作。编辑:它也适用于gcc4.8.1。 最佳答案 是的,您可以提供参数名称。来自C++11标准的第8.3.5/11段:Anidentifiercanoptionallybep

c++ - 为什么没有 std::make_function()?

std::function是几乎所有可调用事物的有用包装器,包括自由函数、lambda、仿函数、成员函数,结果来自std::bind.但是,当创建std::function,必须明确指定函数签名,如(取自here)structFoo{Foo(intnum):num_(num){}voidprint_add(inti)const{std::coutf_display=print_num;//storealambdastd::functionf_display_42=[](){print_num(42);};//storetheresultofacalltostd::bindstd::fu

c++ - 为什么没有 std::make_function()?

std::function是几乎所有可调用事物的有用包装器,包括自由函数、lambda、仿函数、成员函数,结果来自std::bind.但是,当创建std::function,必须明确指定函数签名,如(取自here)structFoo{Foo(intnum):num_(num){}voidprint_add(inti)const{std::coutf_display=print_num;//storealambdastd::functionf_display_42=[](){print_num(42);};//storetheresultofacalltostd::bindstd::fu

c++ - 最佳实践 : Where should function comments go in C/C++ code?

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter寻求指导。关闭9年前。所以...我知道这可能是主观的,但我想就什么是最佳做法提出一些意见。假设我有以下header和.cpp文件:标题://foo.hclassfoo{public:intbar(intin);};cpp://foo.cppintfoo::bar(intin){//somealgorithmherewhichmodifiesinandreturnsthemo

c++ - 最佳实践 : Where should function comments go in C/C++ code?

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter寻求指导。关闭9年前。所以...我知道这可能是主观的,但我想就什么是最佳做法提出一些意见。假设我有以下header和.cpp文件:标题://foo.hclassfoo{public:intbar(intin);};cpp://foo.cppintfoo::bar(intin){//somealgorithmherewhichmodifiesinandreturnsthemo

c++ - 当 std::function<X()> 是 X 类的成员时,为什么不能编译它?

以下代码无法编译:#includestructX{std::function_gen;};intmain(){Xx;x._gen=[]{returnX();};//thislineiscausingproblem!}我不明白为什么分配给x._gen会导致问题。两个gcc和clang正在给出类似的错误消息。谁能解释一下?编译器错误信息GCC'serror:Infileincludedfrommain.cpp:1:0:/usr/include/c++/4.8/functional:Ininstantiationof‘std::function::_Requires::_CheckResul

c++ - 当 std::function<X()> 是 X 类的成员时,为什么不能编译它?

以下代码无法编译:#includestructX{std::function_gen;};intmain(){Xx;x._gen=[]{returnX();};//thislineiscausingproblem!}我不明白为什么分配给x._gen会导致问题。两个gcc和clang正在给出类似的错误消息。谁能解释一下?编译器错误信息GCC'serror:Infileincludedfrommain.cpp:1:0:/usr/include/c++/4.8/functional:Ininstantiationof‘std::function::_Requires::_CheckResul

c++ - gcc 中非 MoveConstructible 函数对象的 std::function

我正在尝试与std::function相处。来自引用here可以看到std::function的ctor的参数应该是可调用的并且是可复制构造的。所以这里是一个小例子:#include#include#includeclassA{public:A(inta=0):a_(a){}A(constA&rhs):a_(rhs.a_){}A(A&&rhs)=delete;voidoperator()(){std::coutFunction;intmain(intargc,char*argv[]){std::cout::value::value我们有可调用、可复制构造但不可移动构造的类。我相信这足以

c++ - gcc 中非 MoveConstructible 函数对象的 std::function

我正在尝试与std::function相处。来自引用here可以看到std::function的ctor的参数应该是可调用的并且是可复制构造的。所以这里是一个小例子:#include#include#includeclassA{public:A(inta=0):a_(a){}A(constA&rhs):a_(rhs.a_){}A(A&&rhs)=delete;voidoperator()(){std::coutFunction;intmain(intargc,char*argv[]){std::cout::value::value我们有可调用、可复制构造但不可移动构造的类。我相信这足以

c++ - 如何使用自定义分配器创建 std::function?

为了节省一些代码,假设我有一个名为MyAlloc的自定义分配器我已经成功地使用了std::vector如下:std::vector>vec;现在我想使用自定义分配器在std::function中保存一个lambda,我该怎么做?我的失败尝试:inti[100];std::functionf(MyAlloc{},[i](intin){//...});更新:std::function中的分配器为deprecated 最佳答案 按照标准,你需要给一个tagtype作为第一个参数表明您要使用自定义分配器:std::functionf(std