shuisheng said:
Array<class T, size_t n0, size_t n1, size_t n2 ...>
The dimension can be arbitrary. Is it possible?
Unfortunately not.
There is a language proposal for variable-length template arguments,
since this is something that comes up frequently.
You're current choices are to wrap the sizes into one type and pass
only that type. This would, however, uglify usage, and this wrapping
type would need to be constructed first:
Array< MyClass, vector< int_< 42 >, int_< 17 > > > some_array;
The vector and int_ are from the boost mpl library and live in
boost::mpl. The loki library provides similar facilities.
Another possibility is to simple make a lot of defaults and
specialise:
template< class T, int n0 = 0, int n1 = 0, int n2 = 0 >
struct Array
{
// 3-dim version
};
template< class T, int n0 = 0, int n1 = 0 >
struct Array< T, n0, n1, 0 >
{
// 2-dim version
};
template< class T, int n0 = 0 >
struct Array< T, n0, 0, 0 >
{
// 1-dim version
};
(I used zero to mean that the dimension should be used at all, so a
dimension can't be zero in the proper sense anymore.)
That is of course a typing and maintenance nightmare, so generally
people use preprocessor metaprogramming to alleviate that. boost also
provides a library to make that feasible. The idea is to define each
of those block as a macro and have that be automatically expanded for
each number of parameters.
None of this is nice, but they're the current options I'm afraid.
So, I don't know what exactly you want to do, but probably there is
another way around it. You might want to consider templating only on
the number of dimensions and have their sizes be runtime
configuration. You lose a bit of type safety, but it becomes much more
wieldy.
Regards,
Jens