By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

Problem on class template.

P: n/a
Dear All,

I want to get a array template like

Array<class T, size_t n0, size_t n1, size_t n2 ...>

The dimension can be arbitrary. Is it possible?

Thanks for your help.

Shuisheng

Sep 23 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

shuisheng wrote:
Dear All,

I want to get a array template like

Array<class T, size_t n0, size_t n1, size_t n2 ...>

The dimension can be arbitrary. Is it possible?

Thanks for your help.

Shuisheng
What is the purpose of the array template? What does a std::vector lack
which does not fullfill your needs?

#include <iostream>
#include <vector>

int main()
{
std::vector< double v; // replace double with whatever
v.push_back( 11.1 );
std::cout << "vector size = " << v.size();
}

Sep 23 '06 #2

P: n/a
"shuisheng" <sh*********@yahoo.comwrites:
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
Sep 24 '06 #3

P: n/a

Jens Theisen 写道:
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
};
So this is a template Array< class T, int n0 = 0, int n1 = 0, int n2 =
0 >. I can write such as Array<int>, Array<int, 2>, Array<int, 2, 2>
or Array<int, 2, 2, 2>. Right?

template< class T, int n0 = 0, int n1 = 0 >
struct Array< T, n0, n1, 0 >
{
// 2-dim version
};
This is a specialization. It defines the cases when n2 = 0. Right?

template< class T, int n0 = 0 >
struct Array< T, n0, 0, 0 >
{
// 1-dim version
};
A deeper specilization defines the cases when n1 and n2 = 0. Right?

(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.)
I still don't understand the zeros mean that the dimension should be
used at all. Wound you give me a little more explanation?

I appreciate your nice suggestion.

Bests,

Shuisheng

Sep 24 '06 #4

P: n/a
"shuisheng" <sh*********@yahoo.comwrites:
template< class T, int n0 = 0 >
struct Array< T, n0, 0, 0 >
{
// 1-dim version
};

A deeper specilization defines the cases when n1 and n2 = 0. Right?
Yes to this and all your previous questions.
(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.)

I still don't understand the zeros mean that the dimension should be
used at all. Wound you give me a little more explanation?
Sorry, I meant: should _not_ be used at all.

The thing I wanted to point out that, while

Array< T, n0 >

is a 1-dim array,

Array< T, n0, 0 >

will also be a 1-dim array, and not an array with the second dimension
being zero. You need to have a special value you can give as a default
and specialise on - and for ints, there is not much choice. You could
also take some very large value or -1, and there are some other ways
as well, but I guess in this case, the specialising on 0 or -1 is
reasonable.

Regards,

Jens
Sep 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.