In C++17 there's std::make_from_tuple
; However that only applies to situations where the number of the elements stored in the tuple matches with that of the constructor.
I was able to concatenate two tuples and use std::make_from_tuple
.
#include <iostream>
#include <utility>
#include <tuple>
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 = std::make_from_tuple<ConstructMe>(
std::tuple_cat(std::make_tuple(a), part)
);
return 0;
}
And after some research ( this question I was able to make it work with references as well with std::tie
as well.
#include <iostream>
#include <utility>
#include <tuple>
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 = std::make_from_tuple<ConstructMe>(
std::tuple_cat(std::tie(a), part)
);
return 0;
}
Is there a simpler way to do this ( e.g. with std::bind
) which doesn't require c++17?