Parameter pack, capture clause and initializers - c++

Note: this question is for the sake of curiosity.
Consider the documentation for the lambda and the one for the parameter pack.
The following code is legal:
template<typename... T>
void f(T... t) {
auto lambda = [t...](){ /* do something */ };
// do something else
}
The same goes without saying for this one:
void f(int i) {
auto lambda = [i = i](){ /* do something */ };
// do something else
}
I was wondering if it's possible to define an initializer in the capture clause for a parameter pack.
Something like this:
template<typename... Args>
void f(Args&&... args) {
auto lambda = [params = std::forward<Args>(args)...](){ /* do something */ };
// do something else
}
Ok, I suspect it doesn't make sense, but it gives an idea at least.
Is there any viable solution to do that?
Please, do not ask me why I would do that. I don't want to do that. As I said, it's for the sake of curiosity.

Nope.
Best you can do is:
template<typename... Args>
void f(Args&&... args) {
auto lambda = [params = std::make_tuple(std::forward<Args>(args)...)]()
{ /* do something */ };
// do something else
}
then interact with params as a tuple, including using get.
I find you end up having to leave the world of lambdas to unpack the elements again. Probably you could do it with a helper like this:
template<std::size_t...Is, class F>
decltype(auto) unpack_impl( std::index_sequence<Is...>, F&& f ) {
return std::forward<F>(f)(std::integral_constant<std::size_t, Is>{}...);
}
template<std::size_t N, class F>
decltype(auto) unpack( F&& f ) {
return unpack_impl( std::make_index_sequence<N>{}, std::forward<F>(f) );
}
which takes a template non-type argument N, then generates a pack of integral_constants with constexpr operator size_t and values 0 through N-1, and passes those to a lambda you pass unpack.
Example use:
template<typename... Args>
auto print_later(Args&&... args) {
auto lambda = [params = std::make_tuple(std::forward<Args>(args)...)](
auto&& stream
)
{
unpack<sizeof...(Args)>( [&](auto...Is){
using discard=int[];
(void)discard{0,(void(
stream << std::get<Is>( params )
),0)...
};
});
};
return lambda;
}
Notice the Is being passed to the inner lambda. Basically this technique let us unpack a parameter pack and get its values in an expandible context without having to create a new explicit template function; instead, we create a variadic lambda, and use the types of the arguments (or constexpr operations on them) to get the unpack results.
The above is a function that takes a package of arguments, and returns a function that takes a stream and prints them all.
live example
The unpack function can be made much more generic; at least, have it take a integer_sequence, and in the end even a variation could take a pack of types directly.

Related

Expand parameter pack with index using a fold expression

I've got a template function taking a parameter pack. I want to expand it into calls to a second function while also supplying the index of the item in the pack. I probably can work out how to do it with recursion but I would like to try do it with a fold expression.
This is the function I want the parameter pack to expand into
template<typename T>
void addToRecord(Record& rec, int idx, T&& val)
{
// Do some stuff.
}
And this is the function that takes the parameter pack
template<typename... ARGS>
void addRecord(ARGS&& ...values)
{
Record rec;
// addToRecord(rec, ??????) How do expand 'values' here addToRecord with index of each item?
}
Is this possible? I realize this isn't critical but I'm also trying to get better with using fold expressions.
In addition to another answer let me mention a simpler approach that doesn't need a helper function:
template<typename... Args>
void addRecord(Args&&... values) {
Record rec;
int i = 0;
(addToRecord(rec, i++, std::forward<Args>(values)), ...);
}
The comma operator , guarantees that all addToRecord()s will be called in order:
In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before evaluation of the expression E2 begins.
You can write a helper function that takes a compile time integer sequence, along with the values
template<typename... Args, std::size_t... Is>
void addRecord_impl(Record& rec, std::index_sequence<Is...>, Args && ...values)
{
(addToRecord(rec, Is, std::forward<Args>(values)), ...); // Is and values will be expanded in lock step
}
and then call this helper like this
template<typename... Args>
void addRecord(Args && ...values)
{
Record rec;
addRecord_impl(rec,
std::index_sequence_for<Args...>{},
std::forward<Args>(values)...);
}
Here's a demo
When I need to do this, I like doing it inline.
template<std::size_t...Is>
auto indexer_over( std::index_sequence<Is...> ) {
return [](auto&& f)->decltype(auto){
return f( std::integral_constant<std::size_t, Is>{}... );
};
}
template<std::size_t N, class F>
auto index_upto( F&& f ) {
return indexer_over(std::make_index_sequence<N>{})(std::forward<F>(f));
}
then your code is:
template<typename... ARGS>
void addRecord(ARGS&& ...values)
{
Record rec;
index_upto<sizeof...(ARGS)>( [&](auto...Is) {
( addToRecord(rec, Is), ... );
} );
}
this has the advantage over #evg's solution that the Is are compile time constants for each call to addToRecord, if that matters.
In c++20 you can get the compile time collection of Is values without helpers.
[&]<std::size_t...Is>(std::index_sequence<Is...>) {
( addToRecord(rec,Is), ... );
}( std::make_index_sequence<sizeof...(ARGS)>{} );

Dynamic Dispatch to Template Function C++

I have a template function (in my case a cuda kernel), where there are a small number of boolean template parameters that can chosen between at runtime. I am happy to instantiate all permutations at compile time and dispatch dynamically, like so (for boolean b0,b1,b2):
if (b0) {
if (b1) {
if (b2) {
myFunc<true,true,true,otherArgs>(args);
} else {
myFunc<true,true,false,otherArgs>(args);
}
} else {
if(b2) {
myFunc<true,false,true,otherArgs>(args);
} else {
myFunc<true,false,false,otherArgs>(args);
}
}
} else {
if(b1) {
if(b2) {
myFunc<false,true,true,otherArgs>(args);
} else {
myFunc<false,true,false,otherArgs>(args);
}
} else {
if(b2) {
myFunc<false,false,true,otherArgs>(args);
} else {
myFunc<false,false,false,otherArgs>(args);
}
}
}
This is annoying to write, and gets exponentially worse if I end up with a b3 and b4.
Is there a simple way to rewrite this in a more concise way in C++11/14 without bringing in large external libraries (like boost)? Something like:
const auto dispatcher = construct_dispatcher<bool, 3>(myFunc);
...
dispatcher(b0,b1,b2,otherArgs,args);
No problem.
template<bool b>
using kbool = std::integral_constant<bool, b>;
template<std::size_t max>
struct dispatch_bools {
template<std::size_t N, class F, class...Bools>
void operator()( std::array<bool, N> const& input, F&& continuation, Bools... )
{
if (input[max-1])
dispatch_bools<max-1>{}( input, continuation, kbool<true>{}, Bools{}... );
else
dispatch_bools<max-1>{}( input, continuation, kbool<false>{}, Bools{}... );
}
};
template<>
struct dispatch_bools<0> {
template<std::size_t N, class F, class...Bools>
void operator()( std::array<bool, N> const& input, F&& continuation, Bools... )
{
continuation( Bools{}... );
}
};
Live example.
So kbool is a variable with represents a compile time constant boolean. dispatch_bools is a helper struct that has an operator().
This operator() takes an array of runtime bools, and starting at max-1 proceeds to spawn max if/else branches, each recursing into call to dispatch_bools with one more compile-time bool calculated.
This generates 2^max code; exactly the code you don't want to write.
The continuation is passed all the way down to the bottom recursion (where max=0). At that point, all of the compile-time bools have been built up -- we call continuation::operator() passing in those compile-time bools as function parameters.
Hopefully continuation::operator() is a template function that can accept compile-time bools. If it is, there are 2^max instantiations of it, each with each of the 2^max possible true/false combinations.
To use this to solve your problem in c++14 you just do:
std::array<bool, 3> bargs={{b0, b1, b2}};
dispatch_bools<3>{}(bargs, [&](auto...Bargs){
myFunc<decltype(Bargs)::value...,otherArgs>(args);
});
This is easy because c++14 has auto lambdas; it can have a template operator() on a lambda. Turning those compile-time bool arguments back into template non-type arguments is easy.
Note that many nominally c++11 compilers support auto-lambdas, because of how easy it was. However, if you lack it, you can still solve this in c++11 with a helper struct:
template<class OtherArgs>
struct callMyFunc {
Args args;
template<class...Bools>
void operator()(Bools...){
myFunc<Bools::value...,otherArgs>(args);
}
};
now use is:
std::array<bool, 3> bargs={{b0, b1, b2}};
dispatch_bools<3>{}(bargs, callMyFunc<otherArgs>{args});
This is basically manually writing what the c++14 lambda would do.
In c++14 you can replace void with auto and return instead of just recursing and it will deduce a return type for you reasonably well.
If you want that feature in c++11 you can either write a lot of decltype code, or you can use this macro:
#define RETURNS(...) \
noexcept(noexcept(__VA_ARGS__)) \
-> decltype(__VA_ARGS__) \
{ return __VA_ARGS__; }
and write the body of dispatch_bools like:
template<class T, std::size_t N, class F, class...Bools>
auto operator()( std::array<T, N> const& input, F&& continuation, Bools... )
RETURNS(
(input[max-1])?
dispatch_bools<max-1>{}( input, continutation, kbool<true>{}, Bools{}... )
:
dispatch_bools<max-1>{}( input, continutation, kbool<false>{}, Bools{}... )
)
and similar for the <0> specialization, and get c++14 style return deduction in c++11.
RETURNS makes deducing return types of one-liner functions trivial.
Is there a simple way? No. Can it be done using an unholy mess of garbled templates? Sure, why not.
Implementation
First, this is going to be a bit easier if we have a class rather than a function, simply because parameterized classes can be passed as template parameters. So I'm going to write a trivial wrapper around your myFunc.
template <bool... Acc>
struct MyFuncWrapper {
template <typename T>
void operator()(T&& extra) const {
return myFunc<Acc...>(std::forward<T&&>(extra));
}
};
This is just a class for which MyFuncWrapper<...>()(extra) is equivalent to myFunc<...>(extra).
Now let's make our dispatcher.
template <template <bool...> class Func, typename Args, bool... Acc>
struct Dispatcher {
auto dispatch(Args&& args) const {
return Func<Acc...>()(std::forward<Args&&>(args));
}
template <typename... Bools>
auto dispatch(Args&& args, bool head, Bools... tail) const {
return head ?
Dispatcher<Func, Args, Acc..., true >().dispatch(std::forward<Args&&>(args), tail...) :
Dispatcher<Func, Args, Acc..., false>().dispatch(std::forward<Args&&>(args), tail...);
}
};
Whew, there's quite a bit to explain there. The Dispatcher class has two template arguments and then a variadic list. The first two arguments are simple: the function we want to call (as a class) and the "extra" argument type. The variadic argument will start out empty, and we'll use it as an accumulator during the recursion (similar to an accumulator when you're doing tail call optimization) to accumulate the template Boolean list.
dispatch is just a recursive template function. The base case is when we don't have any arguments left, so we just call the function with the arguments we've accumulated so far. The recursive case involves a conditional, where we accumulate a true if the Boolean is true and a false if it's false.
We can call this with
Dispatcher<MyFuncWrapper, TypeOfExtraArgument>()
.dispatch(extraArgument, true, true, false);
However, this is a bit verbose, so we can write a macro to make it a bit more approachable.1
#define DISPATCH(F, A, ...) Dispatcher<F, decltype(A)>().dispatch(A, __VA_ARGS__);
Now our call is
DISPATCH(MyFuncWrapper, extraArgument, true, true, false);
Complete Runnable Example
Includes a sample myFunc implementation.
#include <utility>
#include <iostream>
#define DISPATCH(F, A, ...) Dispatcher<F, decltype(A)>().dispatch(A, __VA_ARGS__);
template <bool a, bool b, bool c, typename T>
void myFunc(T&& extra) {
std::cout << a << " " << b << " " << c << " " << extra << std::endl;
}
template <bool... Acc>
struct MyFuncWrapper {
template <typename T>
void operator()(T&& extra) const {
return myFunc<Acc...>(std::forward<T&&>(extra));
}
};
template <template <bool...> class Func, typename Args, bool... Acc>
struct Dispatcher {
auto dispatch(Args&& args) const {
return Func<Acc...>()(std::forward<Args&&>(args));
}
template <typename... Bools>
auto dispatch(Args&& args, bool head, Bools... tail) const {
return head ?
Dispatcher<Func, Args, Acc..., true >().dispatch(std::forward<Args&&>(args), tail...) :
Dispatcher<Func, Args, Acc..., false>().dispatch(std::forward<Args&&>(args), tail...);
}
};
int main() {
DISPATCH(MyFuncWrapper, 17, true, true, false);
DISPATCH(MyFuncWrapper, 22, true, false, true);
DISPATCH(MyFuncWrapper, -9, false, false, false);
}
Closing Notes
The implementation provided above will let myFunc return values as well, although your example only included a return type of void, so I'm not sure you'll need this. As written, the implementation requires C++14 for auto return types. If you want to do this under C++11, you can either change all the return types to void (can't return anything from myFunc anymore) or you can try to hack together the return types with decltype. If you want to do this in C++98, ... ... ... ... good luck
1 This macro is susceptible to the comma problem and thus won't work if you pass it zero Booleans. But if you're not going to pass any Booleans, you probably shouldn't be going through this process anyway.

hana::tuple to auto && ... args

Is there a way to use something like :
constexpr auto foo = hana::make_tuple(hana::type_c<Foo1>,hana::type_c<Foo2>);
with something like:
template < typename ... Ts >
struct Final {
constexpr Final(Ts && ... args) {}
};
hana::unpack(foo, [] (auto && ... args) { return Final(args...); });
Because with that code, unpack can't deduce lambda/function type.
Basically I want to create a type which takes a list of arguments but I have a tuple which contains the arguments.
The problem is in your lambda:
[](auto && ... args){ return Final(args...); }
// ~~~~~~~
Final isn't a type, it's a class template. As such, you need to explicitly provide the types. Something like:
[](auto&&... args){ return Final<decltype(args)...>(
std::forward<decltype(args)>(args)...); }
In C++17, with template deduction for class template parameters, the Ts&& does not function as a forwarding reference (see related answer), so the implicit deduction guide would not match your usage anyway as you are only providing lvalues and the guide requires revalues. But this would work:
[](auto... args){ return Final(std::move(args)...); }
If I understand your question correctly, what you're actually looking for is
template <typename ...Ts>
struct Final { ... };
constexpr auto foo = hana::make_tuple(hana::type_c<Foo1>,hana::type_c<Foo2>);
auto final_type = hana::unpack(foo, [](auto ...args) {
return Final<typename decltype(args)::type...>;
});
// now, final_type is a hana::type<Final<Foo1, Foo2>>
You can also achieve the same thing using hana::template_:
constexpr auto foo = hana::make_tuple(hana::type_c<Foo1>,hana::type_c<Foo2>);
auto final_type = hana::unpack(foo, hana::template_<Final>);
The problem I see with Barry's answer is that you'll end up creating a Final<decltype(hana::type_c<Foo1>), decltype(hana::type_c<Foo2>)>, which is probably not what you want.

How to bind variadic template params to function

I'm trying to mimic std::thread constructor functionality:
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
I've tried stepping with debugger to see how it works but I couldn't figure it out.
How can I create and store bind type like thread's constructor does ?
Something like this (the syntax maybe wrong):
class myClass{
private:
auto bindType;
public:
template< class Function, class... Args >
explicit myClass( Function&& f, Args&&... args ) : bindType(somehowBind(f, args) {}
void evaluate() {bindType();}
};
Example of usage:
int test(int i) {return i;}
int main(){
myClass my(test, 5);
my.evaluate();
}
Note that I don't care if somehowBind function will ignore the return type i.e. its return type can be something like std::function.
All I wan't to do is understand how I can bind class... Args to a given function f such that after calling somehowBind it will act like std::bind does.
To clarify my point you can think about what I'm trying to achieve as follow:
thread t(test, 5); // unlike the usual std:::thread, this one is created in suspended mode therefore I need somehow to bind `f` with `5` and store it
t.start(); // now t is executed
It's kinda reminds C# and Java threads, they not executed right after construction.
For starters, to bind some parameters to a function using std::bind you simpy do:
// Some function.
void printValues(int x, double y) {
std::cout << x << " " << y << std::endl;
}
auto func = std::bind(printValues, 5, 2.0); // Bind params and return functor.
func(); // Evaluate function call (returns void in this case).
Next, to store a functor and its parameters in a class and you don't care about the return value when evaluating then simply use a lambda expression to wrap the std::bind expression (the lambda is used to drop the return value):
struct Foo {
template <typename Function, typename... Args>
Foo(Function&& func, Args&&... args) {
auto f = std::bind(std::forward<Function>(func), std::forward<Args>(args)...);
func_ = [f] { f(); };
// func_ = [f{std::move(f)}] { f(); }; // In C++14 you can move capture.
}
void evaluate() { func_(); }
std::function<void()> func_;
};
Also see this live example
If you're looking to store a variadic pack then see this answer: How to store variadic template arguments?

Function wrapper that works for all kinds of functors without casting

I'd like to create a function that takes a weak pointer and any kind of functor (lambda, std::function, whatever) and returns a new functor that only executes the original functor when the pointer was not removed in the meantime (so let's assume there is a WeakPointer type with such semantics). This should all work for any functor without having to specify explicitly the functor signature through template parameters or a cast.
EDIT:
Some commenters have pointed out that std::function - which I used in my approach - might not be needed at all and neither might the lambda (though in my original question I also forgot to mention that I need to capture the weak pointer parameter), so any alternative solution that solves the general problem is of course is also highly appreciated, maybe I didn't think enough outside the box and was to focused on using a lambda + std::function. In any case, here goes what I tried so far:
template<typename... ArgumentTypes>
inline std::function<void(ArgumentTypes...)> wrap(WeakPointer pWeakPointer, const std::function<void(ArgumentTypes...)>&& fun)
{
return [=] (ArgumentTypes... args)
{
if(pWeakPointer)
{
fun(args...);
}
};
}
This works well without having to explicitly specify the argument types if I pass an std::function, but fails if I pass a lambda expression. I guess this because the std::function constructor ambiguity as asked in this question. In any case, I tried the following helper to be able to capture any kind of function:
template<typename F, typename... ArgumentTypes>
inline function<void(ArgumentTypes...)> wrap(WeakPointer pWeakPointer, const F&& fun)
{
return wrap(pWeakPointer, std::function<void(ArgumentTypes...)>(fun));
}
This now works for lambdas that don't have parameters but fails for other ones, since it always instantiates ArgumentTypes... with an empty set.
I can think of two solution to the problem, but didn't manage to implement either of them:
Make sure that the correct std::function (or another Functor helper type) is created for a lambda, i.e. that a lambda with signature R(T1) results in a std::function(R(T1)) so that the ArgumentTypes... will be correctly deduced
Do not put the ArgumentTypes... as a template parameter instead have some other way (boost?) to get the argument pack from the lambda/functor, so I could do something like this:
-
template<typename F>
inline auto wrap(WeakPointer pWeakPointer, const F&& fun) -> std::function<void(arg_pack_from_functor(fun))>
{
return wrap(pWeakPointer, std::function<void(arg_pack_from_functor(fun))(fun));
}
You don't have to use a lambda.
#include <iostream>
#include <type_traits>
template <typename F>
struct Wrapper {
F f;
template <typename... T>
auto operator()(T&&... args) -> typename std::result_of<F(T...)>::type {
std::cout << "calling f with " << sizeof...(args) << " arguments.\n";
return f(std::forward<T>(args)...);
}
};
template <typename F>
Wrapper<F> wrap(F&& f) {
return {std::forward<F>(f)};
}
int main() {
auto f = wrap([](int x, int y) { return x + y; });
std::cout << f(2, 3) << std::endl;
return 0;
}
Assuming the weak pointer takes the place of the first argument, here's how I would do it with a generic lambda (with move captures) and if C++ would allow me to return such a lambda:
template<typename Functor, typename Arg, typename... Args>
auto wrap(Functor&& functor, Arg&& arg)
{
return [functor = std::forward<Functor>(functor)
, arg = std::forward<Arg>(arg)]<typename... Rest>(Rest&&... rest)
{
if(auto e = arg.lock()) {
return functor(*e, std::forward<Rest>(rest)...);
} else {
// Let's handwave this for the time being
}
};
}
It is possible to translate this hypothetical code into actual C++11 code if we manually 'unroll' the generic lambda into a polymorphic functor:
template<typename F, typename Pointer>
struct wrap_type {
F f;
Pointer pointer;
template<typename... Rest>
auto operator()(Rest&&... rest)
-> decltype( f(*pointer.lock(), std::forward<Rest>(rest)...) )
{
if(auto p = lock()) {
return f(*p, std::forward<Rest>(rest)...);
} else {
// Handle
}
}
};
template<typename F, typename Pointer>
wrap_type<typename std::decay<F>::type, typename std::decay<Pointer>::type>
wrap(F&& f, Pointer&& pointer)
{ return { std::forward<F>(f), std::forward<Pointer>(pointer) }; }
There are two straightforward options for handling the case where the pointer has expired: either propagate an exception, or return an out-of-band value. In the latter case the return type would become e.g. optional<decltype( f(*pointer.lock(), std::forward<Rest>(rest)...) )> and // Handle would become return {};.
Example code to see everything in action.
[ Exercise for the ambitious: improve the code so that it's possible to use auto g = wrap(f, w, 4); auto r = g();. Then, if it's not already the case, improve it further so that auto g = wrap(f, w1, 4, w5); is also possible and 'does the right thing'. ]