473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problem on class template.

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
4 1521

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

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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
7
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
0
by: Jon Slaughter | last post by:
I'm having a big problem(its probably simple but I just can't seem to figure it out). I have a Node template: template <unsigned int I, unsigned int J, typename T> struct Node { enum {i =...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
2
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
7
by: StephQ | last post by:
First of all: distinction of keywords typename and class in template arguments. Accoarding to a post in a well known moderated group: "There are three possibilities for template arguments: 1)...
3
by: StephQ | last post by:
I'm writing some algorithms that works on generic functions using boost::bind. My problem is that class templates can never be deduced. In the simplest cases I just write the class like: ...
3
by: knkk | last post by:
I am trying to include this code in the footer.template files of all blog templates: <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.