是否可以重新路由lambda以将其包装在包装器类中
Is it possible to reroute lambda to wrap it in a wrapper class?
我正在修补std::function
的矢量:
#include "stdafx.h"
#include <stdexcept>
#include <functional>
#include <iostream>
#include <vector>
typedef std::function<void(int)> FuncType;
std::vector<FuncType> container;
int _tmain(int argc, _TCHAR* argv[])
{
container.push_back([](int i){std::cout << i+1 << std::endl;});
container.push_back([](int i){std::cout << i+42 << std::endl;});
for(auto & o : container)
{
o(4);
}
return 0;
}
基本上只是返回5和46,并在考虑是否可以将容器的声明更改为某种包装器类,但要保持lambdas的推回(=不改变除声明外的任何其他内容)。
目前我试图实现一些存根包装不做什么特别应该只是编译,但似乎从lambda到wrapper的转换不能隐式完成。
#include "stdafx.h"
#include <stdexcept>
#include <functional>
#include <iostream>
#include <vector>
typedef std::function<void(int)> FuncType;
template<class T>
class Wrapper
{
public:
Wrapper(T t)
{
_t = t;
}
void operator()(int i) const
{
_t(i);
}
protected:
T & _t;
};
std::vector<Wrapper<FuncType>> container; // Only line changed
int _tmain(int argc, _TCHAR* argv[])
{
container.push_back([](int i){std::cout << i+1 << std::endl;});
container.push_back([](int i){std::cout << i+42 << std::endl;});
for(auto & o : container)
{
o(4);
}
return 0;
}
这里的目标是包装对o(int)
的调用并输出一些诊断,例如:o.target_type().name()
或性能值等,但没有改变push_back到容器包装(避免宏魔术)
注意:由于我使用的是VS 2012,可变模板参数尚未实现,标准MS std::function
采用了一些宏魔术,如_VARIADIC_EXPAND_P1_1(_CLASS_FUNC_CLASS_1, , , , )
来提供operator()
您正在尝试进行两个用户定义的转换,这在c++中是非法的。相反,让构造函数成为一个受约束的模板。见下文:
#include <functional>
#include <utility>
#include <iostream>
#include <vector>
#include <type_traits>
typedef std::function<void(int)> FuncType;
template<class T>
class Wrapper
{
public:
template<typename U,
typename std::enable_if<
std::is_constructible<T, U>::value,
int
>::type = 0>
Wrapper(U t)
: _t(std::move(t))
{}
void operator()(int i) const
{
_t(i);
}
private:
T _t;
};
std::vector<Wrapper<FuncType>> container; // Only line changed
int main(int argc, char* argv[])
{
container.push_back([](int i){std::cout << i+1 << std::endl;});
container.push_back([](int i){std::cout << i+42 << std::endl;});
for(auto & o : container)
{
o(4);
}
}
相关文章:
- 在析构函数之后,围绕成员函数的C++lambda包装器是如何工作的
- 将捕获的 lambda 包装为模板函数中的函数指针
- 为什么将函数包装到 lambda 中可能会使程序更快?
- 返回对 std::函数包装的 lambda 中静态变量的引用会导致段错误
- lambda 和 std::function 的包装器
- C++如何将可变参数函数模板的参数包 -> 包装到 lambda 中
- const std::function 包装一个非 const 运算符() / 可变 lambda
- 在 lambda 中包装并执行packaged_task
- std :: async和std ::包装rvalue参考lambda时的差异
- 返回一个 std::函数包装的 lambda,该 lambda 调用指向成员函数的指定指针
- 用C++lambda包装C回调,可以使用模板多态性
- 语法繁重的多重lambda包装器的替代方案-如何避免样板代码
- 防止在std::函数中使用不同类型包装lambda
- C++ Lambda 包装器:应该更喜欢捕获或通过参数
- 在std::bind中包装lambda以捕获r值
- Boost函数和lambda来包装函数
- 是否可以重新路由lambda以将其包装在包装器类中
- 包装lambda表达式
- 在GCC和Clang下,带有lambda的简单RAII包装器的复制初始化意外失败
- 在lambda包装器中捕获递归函数的指针