I was tinkering with a vector of std::function
like this:
#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;
}
which basically just returns 5 and 46 and was thinking whether I can change the declaration of the container to some sort of wrapper class but to maintain the push back of the lambdas (= not changing anything else except declaration).
Currently I tried to implement some stub wrapper doing nothing in particular which should just compile, but it seems that the conversion from lambda to Wrapper cannot be done implicitly.
#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;
}
The goal here is to wrap the call to o(int)
and output some diagnostics e.g.
o.target_type().name()
or performance values etc but without altering the push_back into the container to Wrapper (avoiding macro magic too)
Note: As I am using VS 2012 where variadic template arguments are not yet implemented, the standard MS std::function
resorted to some macro magic like _VARIADIC_EXPAND_P1_1(_CLASS_FUNC_CLASS_1, , , , )
to provider operator()