It seems clunky and not very user-friendly. Is it possible to reduce this to a single line in C++03?
Since you are basically imitating std::array
, just rid of the constructor:
template <typename T, int N>
class N_Tuple
{
public:
T values_[N];
};
Note, in this, you have to make the values_
public.
That way you can initialize them just as you expect:
N_Tuple<int,3> p = {1,2,3};
This is valid in both C++03 and C++11, no extensions of any kind (Tr1, Boost or whatever) required.
Another alternative is to hide the helper array behind a macro. The basic idea is to initialize your helper array and from there your tuple-esque array. But since you'd want the array's contents to come after in the initializing notation (var x = contents) you'd have to use something like a prolog-epilog macro which will require some repetition and you'll have to be careful with the comma in your particular type's case:
SOME_MACRO_BEGIN(My_Sequence_type, t) = {1, 2, 3}
SOME_MACRO_END(My_Sequence_type, t);
I've worked on such a solution that is compatible with both C++03 and C++11 (provided you do implement an initializer_list
constructor for the C++11 case) without any particular requirements on the C++03 side of things.
(I am not sure how would it ever be possible to do it in a single macro, since a list of elements would contain commas which are processed specially for the macro and variadic macros are not a thing in C++03)
But unless your list is short or you abbreviate the names for the prolog-epilog macros a lot, it won't likely fit in a single line.
(Moreover, it still requires copying of the data, even though for the most simple kinds of types that won't ever matter)