I would like to initialize a class with a mix of basic types, arrays and sub-structures like
A a { 1, // int
1.2, // double
"Hello", // const char *
{ 1, 2, 3 }, // array<int>
{ 1.2, 2.4 } // array<double>
{ 1, 1.2, "Hello" } // sub-structure of any types
};
where the sub-structure should be able to contain nested sub-structures as well.
For the basic types I can do that with variadic templates:
#include <iostream>
class A {
public:
template <class T, class... Args>
void print(T v) {
std::cout << v << std::endl;
}
template <class T, class... Args>
void print(T v, Args... args) {
std::cout << v << std::endl;
print(args...);
};
template <class T, class... Args>
A(T v, Args... args) {
print(v, args...);
};
};
int main() {
A a { 1,
1.2,
"Hello",
};
}
which correctly produces
1
1.2
Hello
But I fail for arrays and sub-structures. I'm restricted to C++11. Any help highly appreciated.
Aucun commentaire:
Enregistrer un commentaire