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.
Related
I am trying to solve this problem in C++ TMP where in i need to convert one parameter pack types into another, and then convert back the types and also values. The conversion back part is based on a boolean criteria that whether an arg in Args... was transformed or not in the first place.
Basically, i have a pack(Args...). First, i transform this (for each args[i], call a transform function). It works like this:
For each arg in Args..., just create same type in transformed_args... unless it is one of following, in that case do following conversions:
Type In Args...
Type In transformed_Args...
SomeClass
shared_ptr to SomeClass
std::vector of SomeClass
std::vector of shared_ptr to SomeClass
everything else remains the same for ex:
int remains int
std::string remains std::string
I achieve this by template specialization, of course
For the next part, i take transformed_args..., publish a class and a functor. I receive call back on this functor from(C++generated Python using Pybind, not important though). Relevant bits of that class look like this...
template<typename C, typename...transformed_args..., typename... Args>
class SomeTemplateClass
{
MethodWrapper<C,void, Args...> func;
//.....
void operator()(transformed_args... targs)
{
//....
(*func.wrapped_method_inside)(transform_back_magic(targs)...) // this is want i want to achieve.
//transform_back_magic(targs)... is a plaeholder for code that checks if type of args[i]... != type of targs[i]... and then calls a tranform_back specialization on it else just return args[i].val
}
}
targs are in transformed_args... format, but underlying C++ function they are aimed for expects Args...
template<typename... Args, typename... transformed_args, ........whatever else is needed>
transform_back_magic(....)
{
if(Args[i].type != transformed_args[i].types)
tranform_back(targs[i]...);
}
the tranform_back function template logic is specialized for different cases and all logic is in place. But how to invoke that based on this boolean criteria is hitting my TMP knowledge limits. I just got started not many weeks ago.
Here i am listing down what i have created so far.
First of all this is what i need in pseudo code
template<typename C, typename... transformed_args, typename... Args>
class SomeTemplateClass
{
MethodWrapper<C,void, Args...> func;
void operator(transformed_args... targs)
{
**//In pseudo code, this is what i need**
Args... params = CreateArgsInstanceFromTransformedArgs(targs);
(*func.wrapped_method_inside)(params...);
}
}
In my attempt to implement this, so far I have decided on creating a tuple<Args...> object by copying data from targs(with conversions where ever required)
void operator(transformed_args... targs)
{
//....
auto mytup = call1(std::tuple<args...>(), std::make_index_sequence<sizeof...(Args)>,
std::make_tuple(targs...), targs...);
// mytup can be std::tuple<Args...>(transform_back(1st_targs), transform_back(2nd_targs)....). Once available i can write some more logic to extract Args... from this tuple and pass to(*func.wrapped_method_inside)(....)
(*func.wrapped_method_inside)(ArgsExtractorFromTuple(mytup)); // this part is not implemented yet, but i think it should be possible. This is not my primary concern at the moment
}
//call1
template<typename... Args, typename... Targs, std::size_t... N>
auto call1(std::tuple<Args...> tupA, std::index_sequence<N>..., std::tuple<Targs...> tupT, Targs ..)
{
auto booltup = tuple_creator<0>(tupA, tupT, nullptr); // to create a tuple of bools
auto ret1 = std::make_tuple<Args...>(call2(booltup, targs, N)...); // targs and N are expanded together so that i get indirect access to see the corresponding type in Args...
return ret1;
}
// tuple_creator is a recursive function template with sole purpose to create a boolean tuple.
// such that std::get<0>(booltup) = true,
//if tuple_element_t<0,std::tuple<Args...>> and tuple_element_t<0,std::tuple<targs...>> are same types else false
template<size_t I, typename... Targs, typename... Args>
auto tuple_creator(std::tuple<Args...>tupA, std::tuple<Targs...>tupT, std::enable_if_t<I == sizeof...(targs)>*)
{
return std::make_tuple(std::is_same<std::tuple_element_t<I-1, std::tuple<Targs...>>, std::tuple_element_t<I-1, std::tuple<Args...>>>::value);
}
template<size_t I = 0, typename... Targs, typename... Args>
auto tuple_creator(std::tuple<Args...>tupA, std::tuple<Targs...>tupT, std::enable_if_t<I < sizeof...(targs)>*)
{
auto ret1 = tuple_creator<I+1>(tupA, tupT, nullptr);
if(!I)
return ret1;
auto ret2 = std::is_same<std::tuple_element_t<I-1, std::tuple<Targs...>>, std::tuple_element_t<I-1, std::tuple<Args...>>>::value;
return std::tuple_cat(ret1, std::make_tuple(ret2));
}
template<typename TT, typename Tuple>
auto call2(Tuple boolyup, TT t, std::size_t I)
{
auto ret = transform_back<std::get<I>(booltup)>(t); // error: I is not a compile time constant
return ret;
}
transform_back is a template that uses a bool template param and enable_if based specialization to decide whether transform an argument back or not
below are the transform_back specialization for std::vector. Similarly i have others for when T = Class etc and so on
template<bool sameTypes, typename T>
std::enable_if_t<(is_vector<T>::value, is_shared_ptr<typename T::value_type>::value &&
is_class<remove_cvref_t<typename T::value_type_element_type>>::value
&& sameTypes), T>
transform_back(T val) // it was never transfoemd in first place, return as is
{
return val;
}
template<bool sameTypes, typename T>
std::enable_if_t<(is_vector<T>::value, is_shared_ptr<typename T::value_type>::value
&& is_class<remove_cvref_t<typename T::value_type_element_type>>::value
&& !sameTypes),
typename std::vector<typename T::value_type::element_type>>
transform(T val)
{
std::vector<T::value_type::element_type> t;
for(int i = 0 ; i < val.size(); ++i)
{
typename T::value_type::element_type obj = *val[i];
t.push_back(obj);
}
return t;
}
Both these specialization are same and only differ on sameTypes boolean variable
This code currently errors out in call2 method while trying to using
std::get
auto ret = transform_back<std::get<I>(booltup)>(t); // error: I is not a compile time constant
How can you help?
1)What could be the work around to std::get issue here? Just cant figure out a way to fit in std::size_t as template arg here instead of function arg to make it work at compile time.
Other than this:
2)If you can suggest an alternative approach to implement from top level.
Args... params = CreateArgsInstanceFromTransformedArgs(targs);
That would be great. The path i took is not very convincing personally to me.
If I understand correctly, you might do something like:
template <typename> struct Tag{};
std::shared_ptr<SomeClass> transform_to(Tag<std::shared_ptr<SomeClass>>, const SomeClass& s)
{
return std::make_shared<SomeClass>(s);
}
std::vector<std::shared_ptr<SomeClass>> transform_to(Tag<std::vector<std::shared_ptr<SomeClass>>>, const std::vector<SomeClass>& v)
{
std::vector<std::shared_ptr<SomeClass>> res;
res.reserve(v.size());
for (const auto& s : v) {
res.emplace_back(std::make_shared<SomeClass>(s));
}
return res;
}
const SomeClass& transform_to(Tag<SomeClass>, const std::shared_ptr<SomeClass>& s)
{
return *s;
}
std::vector<SomeClass> transform_to(Tag<std::vector<SomeClass>>, const std::vector<std::shared_ptr<SomeClass>>& v)
{
std::vector<SomeClass> res;
res.reserve(v.size());
for (const auto& s : v) {
res.emplace_back(*s);
}
return res;
}
template <typename T>
const T& transform_to(Tag<T>, const T& t) { return t; } // No transformations
And then
std::function<void (Args...)> func;
template <typename ... transformed_args>
void operator () (transformed_args... targs) const
{
func(transform_to(Tag<Args>(), targs)...);
}
Just explaining the use case here to add some context. Consider these three methods in C++ each represented with the function pointer SomeTemplateClass::func:
void foo(vector<shared_ptr<SomeClass>>) // 1
// Args... = vector<shared_ptr<SomeClass>>, Targs... = vector<shared_ptr<SomeClass>>
void foo(vector<SomeClass>) // 2
// Args... = vector<SomeClass>, Targs... = vector<shared_ptr<SomeClass>>
void foo(vector<SomeClass>, vector<shared_ptr<SomeClass>>) // 3
// Args... = vector<SomeClass>, vector<shared_ptr<SomeClass>>, Targs... = vector<shared_ptr<SomeClass>>, vector<shared_ptr<SomeClass>>
One instance each of SomeTemplateClass is exposed to Python via Pybind. I do these transformations so that when foo is called from Python, any arg vector<T>(in C++) is received as vector<shared_ptr<T>> in SomeTemplateClass functor. This helps in to get handle to previously created objects T that i need.
But as you can see from 3 cases for foo, foo(vector<shared_ptr<T>>) does not need to be transformed to and subsequently not need to be transformed back. The case of 'tranform_to'is easily handled with template specialization, but while transforming back, vector<shared_ptr<T>> cant be blindly converted back to vector<T>. So (transform(targs...)) needs an additional logic to transform a particular arg (or targ) only when targ[i]::type != arg[i]::type
Building on Jarod's answer, i rather need something like this where in transform_to method for vector<shared_ptr> is further divided in two possible templates
template<bool wasOriginallyTransformed>
enable_if<!wasOriginallyTransformed, std::vector<std::shared_ptr<SomeClass>> transform_to(Tag<std::vector<SomeClass>>, const std::vector<std::shared_ptr<SomeClass>>& v)
{
return v;
}
template<bool wasOriginallyTransformed>
enable_if<!wasOriginallyTransformed, std::vector<<SomeClass>
transform_to(Tag<std::vector<SomeClass>>, const std::vector<std::shared_ptr<SomeClass>>& v)
{
std::vector<SomeClass> res;
res.reserve(v.size());
for (const auto& s : v) {
res.emplace_back(*s);
}
return res;
}
Why template variable or template typedef cannot be declared inside of a scope?
I would like to write a code in c++17 like this
auto foo = [](auto fun, auto... x) {
template <typename T>
using ReturnType = std::invoke_result_t<fun, T>;
if constexpr (!(std::is_same_v<void, ReturnType<decltype(x)>> || ... ||
false)) {
return std::tuple<ReturnType<decltype(x)>...>(fun(x)...);
} else {
(fun(x), ...);
return;
}
};
This code defines a function foo which takes a function fun and bunch of arguments x.... If all of the return types fun(x) are not void then return a tuple of results. If atleast one of the return types is void then just call all the functions but return void.
In this simple example, I can of course replace ReturnType<decltype(x)> with decltype(fun(x)), but in my use case the actual type is much more complicated and the above code serves only as a motivation.
Also, I hate writing ReturnType<decltype(x)>. I would much prefer writing ReturnType(x), but that is probably not possible.
Solution I do not like: Define template typedef outside of the function as
template<typename Fun, typename T>
using ReturnType = std::invoke_result_t<Fun,T>;
and then in the function use
ReturnType<decltype(fun),delctype(x)>
Which is getting long and I have to put every local type as a template parameter.
The code is actually simpler without introducing any helpers:
if constexpr ((!std::is_void_v<decltype(fun(x))> && ...)) {
return std::tuple(fun(x)...);
} else {
(fun(x), ...);
}
&& and || have default values for empty packs (true and false, respectively), so you don't have to turn them into unary operators. And you don't need invoke_result_t since you're just directly calling. And even if you did:
using F = decltype(fun);
if constexpr ((!std::is_void_v<std::invoke_result_t<F, decltype(x))> && ...)) {
return std::tuple(std::invoke(fun, x)...);
} else {
(fun(x), ...);
}
Not much longer.
That said, I find this construct not very helpful - given that you get wildly different results for the void and non-void cases. Maybe f(x) is still an X but f(y) is void, we'd get foo(x,x) being tuple<X,X> but foo(x,y) being void? Hard to code around.
I would suggest instead of dropping all the return types, just work around the broken ones. As in:
struct Void { };
template <typename F, typename... Args,
typename R = std::invoke_result_t<F, Args...>,
REQUIRES(std::is_void_v<R>)>
Void invoke_void(F&& f, Args&&... args) {
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
return Void{};
}
template <typename F, typename... Args,
typename R = std::invoke_result_t<F, Args...>,
REQUIRES(!std::is_void_v<R>)>
R invoke_void(F&& f, Args&&... args) {
return std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}
And now, we can always just call the function and return it:
auto foo = [](auto fun, auto... x) {
return std::tuple(invoke_void(fun, x)...);
};
I'm in a bit of a pickle, following up my previous question and using a code similar to the one I posted here.
I use a variadic template function which accepts variadic objects
It packs them into a tuple
Iterates them using the visitor idiom
Binds for each object a callback
Instead of the original minified example shown below:
template <typename... Args>
void make_classes(Args... args)
{
auto t = std::tuple<Args...>(args...);
unsigned int size = std::tuple_size<decltype(t)>::value;
auto execute = [](auto & obj){ obj.operator()(); };
for (int i = 0; i < size; i++) {
visit_at(t, i, execute);
}
}
I am trying to understand how I can deduce the template type of the auto lambda, so that I can bind it:
template <typename... Args>
void make_classes(Args... args)
{
auto t = std::tuple<Args...>(args...);
unsigned int size = std::tuple_size<decltype(t)>::value;
auto execute = [](auto & obj){
// obtain type of obj as T?
auto callback = std::bind(&T::deserialise, obj, std::placeholders::_1);
// do something else here using this callback.
};
for (int i = 0; i < size; i++) {
visit_at(t, i, execute);
}
}
There's a catch: the parameter objects are non-copyable (although I could change that), but I would like to know if/how the above could work by deducing the template type packed in the tuple as obtained by the visitor.
If I can't deduce the type inside the lambda, can I somehow store it within the tuple (e.g.,: type & object) in order to later extract it?
Just use another lambda:
auto callback = [&obj](auto& x){
obj.deserialise(x);
};
std::bind is rarely useful. (If you really want to copy obj, you can drop the leading &.)
Moreover, you don't actually need a tuple...
template <class F, class... Args>
void for_each_arg(F&& f, Args&&... args) {
using swallow = int[];
(void)swallow{0,
(void(f(std::forward<Args>(args))), 0)...
};
}
template <typename... Args>
void make_classes(Args... args)
{
for_each_arg([](auto& obj){
auto callback = [&obj](auto& x) { obj.deserialise(x); };
// do something with callback
}, args...);
}
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.
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'. ]