I created a variadic struct based on a std::tuple
.
Then, I would like to use a C-style array in the member initializer list with automatic type deduction / class template argument deduction.
I created a minimum reproducible example:
#include <iostream>
#include <tuple>
template <typename ... Args>
struct VariadicStruct {
std::tuple<Args...> data{};
VariadicStruct() {}
VariadicStruct(Args...args) {
data = std::make_tuple(args...);
};
// More data and functions
// . . .
};
int main() {
VariadicStruct vs1{ {1,2,3}, 4.0, 'c' }; // Does not work
std::cout << std::get<0>(vs1.data)[0] << '\t' << std::get<1>(vs1.data) << '\t' << std::get<2>(vs1.data) << '\n';
VariadicStruct vs2{ 1, 4.0, 'c' }; // Fine
std::cout << std::get<0>(vs2.data) << '\t' << std::get<1>(vs2.data) << '\t' << std::get<2>(vs2.data) << '\n';
}
I was hoping to create an array in the std::tuple
using a nested braced initializer list. So, in the end, a std::tuple
with int[3]
, double
and char
types.
My guess: Automatic type deduction does not work here.
Is there any way to make this happen?
(This is not a duplicate of this)
With the very good comments from the community, I now implemented the code like the below:
#include <iostream>
#include <tuple>
#include <array>
#include <utility>
template <typename ... Args>
struct VariadicStruct {
std::tuple<Args...> data{};
VariadicStruct() {}
VariadicStruct(Args...args) : data(std::forward<decltype(args)>(args)...) {};
// More data and functions
// . . .
};
int main() {
VariadicStruct vs1{ std::array{1,2,3}, 4.0, 'c' };
std::cout << std::get<0>(vs1.data)[0] << '\t' << std::get<1>(vs1.data) << '\t' << std::get<2>(vs1.data) << '\n';
}