Constructor Arguments from Tuple

Constructor arguments from tuple

C++17 has std::make_from_tuple for this:

template <typename T, typename... Args>
struct foo
{
std::tuple<Args...> args;
T gen() { return std::make_from_tuple<T>(args); }
};

Partial Constructor arguments from tuple

std::bind does something completely different. Quoting cppreference

The function template bind generates a forwarding call wrapper for f. Calling this wrapper is equivalent to invoking f with some of its arguments bound to args.

You can just use the cpp reference link to build your own implementation of the standardized function, e.g. in C++14

#include <iostream>
#include <utility>
#include <tuple>

namespace detail {
template <class T, class Tuple, std::size_t... I>
constexpr T make_from_tuple_impl( Tuple&& t, std::index_sequence<I...> )
{
static_assert(std::is_constructible<T,
decltype(std::get<I>(std::declval<Tuple>()))...>::value);
return T(std::get<I>(std::forward<Tuple>(t))...);
}
} // namespace detail

template <class T, class Tuple>
constexpr T make_from_tuple( Tuple&& t )
{
return detail::make_from_tuple_impl<T>(std::forward<Tuple>(t),
std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{});
}

class ConstructMe{
public:
ConstructMe(int& a_, int b_, int c_)
: a(a_), b(b_), c(c_) { }

private:
int& a;
int b, c;
};

int main(){
int a = 1;
std::tuple<int, int> part = std::make_tuple(2,3);
ConstructMe please = make_from_tuple<ConstructMe>(
std::tuple_cat(std::tie(a), part)
);
}

If you want to go back more language versions, you have to do more by hand. It will be harder and harder, especially if you want to go before C++11...

Construct a tuple by passing a the same variable number of constructor arguments to each element

It would be:

template <typename... Args>
Storage(Args... args) : storage(Ts(args...)...) {}

Purpose of tuple constructor in python

Really the biggest difference between using the literal constructor of () and tuple() is the behavior with iterable vs noniterable arguments.

With the literal representation, you can make a tuple like so:

>>> (1,2)
(1, 2)

You can represent that same tuple without the ():

>>> 1,2
(1, 2)

And a single element tuple, use a trailing comma:

>>> 1,
(1,)
>>> 'abc',
('abc',)

With the tuple function, the single argument must be iterable, and that can surprise at times:

>>> tuple('abc')
('a', 'b', 'c')

For a single element tuple element, you would think you can use a trailing comma, but that still may surprise with the function:

>>> tuple('abc',)
('a', 'b', 'c')

So you have to do:

>>> tuple(('abc',))  # or tuple(['abc'])
('abc',)

Get parameters types of a constructor as tuple

You need to use ConstructorParameters directly on typeof Route

class Route {
constructor(
public method: 'get' | 'post' | 'update' | 'delete',
public path: string,
public handler: () => string,
) { }
}

class Router {
constructor(private routes: (Route | ConstructorParameters<typeof Route>)[] = []) { }
}

Initialize std::tuple with variadic arguments in constructor

The first implementation looks better to me, because you don't have to repeat yourself by providing the function arguments twice.
Because you set the return type to bool, I would recommend changing the name of the class to Predicate, which is a well known term to describe functions that return boolean values.

Note that std::function can take parameter packs as template parameters too, so you can also do this:

std::function<func_ret_ty(FuncArgs...)> m_Function;

Construct tuple by passing the same argument to each element with explicit constructor

In C++ 14, there doesn't seem to be any way of initializing tuple elements when constructors are explicit, because of the is_convertible requirement. I ended up writing a bare-bones implementation of std::tuple myself that is used on C++14 implementations, such as on Debian 8.



Related Topics



Leave a reply



Submit