制作一个复合std::函数
Making a composite std::function
我创建了这个函数Functions
,它用于存储多个std::functions
,并按顺序调用它们,就好像它本身就是std::function
一样:
typedef std::function <void (int)> FuncType;
struct Functions
{
vector<FuncType> m;
void operator+= (FuncType f) {
m.push_back(f);
}
void operator() (int param ){ // same prototype as FuncType
vector<FuncType>::iterator it;
for(it=m.begin(); it!=m.end(); ++it) {
(*it)(param);
}
}
};
它非常有用,因为它可以存储在FuncType
:中
int main()
{
Functions f;
f += foo;
f += bar;
FuncType ptr(f);
ptr(10); // calls foo(10), then bar(10)
}
它工作得很好,但我希望能够使它成为一个模板化的函子。但我想不出一种方法来让operator()
遵循功能的原型:
template <typename FuncType> // for any Function type
struct Functions
{
vector<FuncType> m;
void operator+= (FuncType f) {
m.push_back(f);
}
void operator() (...) { // how do I get the same prototype as FuncType?
vector<FuncType>::iterator it;
for(it=m.begin(); it!=m.end(); ++it) {
(*it)(...);
}
}
};
理想情况下,我还希望有一个辅助函数,它实例化函子(当然,前提是所有函数都有相同的原型),如下所示:
template <typename T>
Functions<T> make_functions( T funcA, T funcB ) {
Functions<T> f;
f += funcA;
f += funcB;
return f;
}
但我不确定编译器是否能够推断出T
是std::函数<>某种
我正在使用std::tr1
template<typename Sig> struct Functions;
template<typename R, typename... Args>
struct Functions<R(Args...)>{
typedef std::function<R(Args...)> FuncType;
std::vector<FuncType> fs;
void operator+=(FuncType f){fs.emplace_back(std::move(f));}
template<typename...Us>
void operator()(Us&&...us){
for(auto&&f:fs)
f(us...);
}
};
int main(){
Functions<void(int)> funcs;
funcs += [](int x){ std::cout<<x<<"n";};
funcs(7);
}
真正完美的转发和收集作为练习留下的回报值。
这样的东西怎么样?编辑:正如所指出的,我错过了make_function
,也错过了与std::forward
完美地进行辩论的机会。http://coliru.stacked-crooked.com/a/f597aacd3b7ce404
#include <functional>
#include <iostream>
#include <vector>
template<typename T, typename... Args>
class Functions
{
private:
std::vector<std::function<T(Args...)>> vec;
public:
void operator += (std::function<T(Args...)> f)
{
vec.push_back(f);
}
void operator()(Args... args)
{
for (auto &f : vec)
{
f(std::forward<Args>(args)...);
}
}
};
int main()
{
Functions<void> funcs;
funcs += []{std::cout<<"Foon";};
funcs += []{std::cout<<"Barn";};
funcs();
std::cout<<"n";
Functions<void, int> funcs2;
funcs2 += [](int a){std::cout<<"Foo"<<a<<"n";};
funcs2 += [](int b){std::cout<<"Bar"<<b<<"n";};
funcs2(2);
}
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- C++11 使用复合键搜索 std::map
- std::函数的复合模式
- C 03中具有std :: bind2nd的复合函数
- 为什么我需要一个类似于复合文字的临时构造来初始化我的std::数组成员
- 制作一个复合std::函数