Problem on class template.

S

shuisheng

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
 
S

Salt_Peter

shuisheng said:
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();
}
 
J

Jens Theisen

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
 
S

shuisheng

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>
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
 
J

Jens Theisen

shuisheng said:
A deeper specilization defines the cases when n1 and n2 = 0. Right?

Yes to this and all your previous questions.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top