Can I make this work?
Imaginary syntax:
auto foo = [] () { };
template <class T, typename ... Args>
auto foo =
[&] (T && V, Args && ... args) {
do_something(V);
foo(std::forward<Args>(args)...);
};
As mentioned in the comments you can't really do this as lambdas aren't powerful enough.
When the [](auto val){}
syntax is allowed this will be much easier.
I use the following for basic calling over tuples.:
template<typename Tuple_t, typename Func_t, std::size_t k_index = 0>
//Only come here if the passed in index is less than sie of tuple
typename std::enable_if<k_index < tuple_size<Tuple_t>::value>::type
call_over_tuple(Tuple_t& irk_tuple, Func_t i_func){
i_func(get<k_index>(irk_tuple));
call_over_tuple<Tuple_t, Func_t, k_index + 1>(irk_tuple, i_func);
}
template<typename Tuple_t, typename Func_t, std::size_t k_index>
typename std::enable_if < k_index >= tuple_size<Tuple_t>::value>::type
call_over_tuple(Tuple_t& irk_tuple, Func_t i_func){
//do nothing
}
Expanding this to just random args gives.
template<typename Func_t, typename ...Args>
void call_over_vals(Func_t i_func, Args&&... i_args){
auto arg_tuple = make_tuple(forward<Args>(i_args)...);
call_over_tuple<decltype(arg_tuple), Func_t>(arg_tuple, i_func);
}
In order to have function overloading or templates you need to create a caller class that does your bidding.
template<typename T>
void do_something(const T& irk_val){
cout << irk_val;
}
class caller_class{
public:
template<typename T>
void operator()(const T& i_val){
do_something(i_val);
}
private:
};
void print_integer(int i_val){
cout << i_val;
}
int main(int argv, char** argc){
call_over_vals(caller_class(), 3, ' ' , 2, " asdf ", 4, "\n");
//If you know the argument type just pass it in
call_over_vals(print_integer, 1, 2, 3, 4, 5);
cout << "\n";
call_over_vals([](int y_val){cout << y_val; }, 1, 2, 3, 4, 5);
}
Output:
3 2 asdf 4
12345
12345
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With