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 forf
. Calling this wrapper is equivalent to invokingf
with some of its arguments bound toargs
.
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
Calling an Explicit Constructor with a Braced-Init List: Ambiguous or Not
Why Can't I Return Bigger Values from Main Function
Can a Copy-Constructor Take a Non-Const Parameter
How to Output to the Console in C++/Windows
C++ Debug Builds Broke in Snow Leopard Xcode
Erase Element in Vector While Iterating the Same Vector
Add Elements to a Vector During Range-Based Loop C++11
How to Implement an Function Equivalent to Bwmorph Matlab Function in Opencv
How to Get the Address of the Std::Vector Buffer Start Most Elegantly
Linking Not Working in Homebrew's Cmake Since Mojave
Visual Studio 2013: Cl.Exe Exited with Code -1073741515
Should Std::Unique_Ptr<Void> Be Permitted
C++ "Hello World" Boost Tee Example Program
How to Write Make_Unique() in VS2012
How Can Adding Code to a Loop Make It Faster
How Does Excel Successfully Round Floating Point Numbers Even Though They Are Imprecise