Question on define.

S

shuisheng

Dear All,

I am wondering may I do the follows or similar ones (if so, how to do
it) in C++.

Assume a template class

template<class _T, int _n>
class Array{};

1) typedef Array Point;

2) typedef Array<double, n> Point<n>;

3) #define Point<n> Array<double, n>

4) #define Point(n) Array<double, n>

Thanks!

Shuisheng
 
R

red floyd

shuisheng said:
Dear All,

I am wondering may I do the follows or similar ones (if so, how to do
it) in C++.

Assume a template class

template<class _T, int _n>
class Array{};

1) typedef Array Point;

2) typedef Array<double, n> Point<n>;

3) #define Point<n> Array<double, n>

4) #define Point(n) Array<double, n>

Thanks!

Shuisheng

#define is "evil" -- it doesn't recognize scope, is completely oblivious
as to what it redefines. Only use it when you can't use anything else:
i.e.: conditional compilation, or macros that use __FILE__ and __LINE__.

3 above won't compile at all, and 4 above is nto a good idea.
Also 1 and 2 above won't compile as well.

Template typedefs don't exist in C++. I'm not sure if they're slated
for C++0x.
 
R

Rolf Magnus

shuisheng said:
I am wondering may I do the follows or similar ones (if so, how to do
it) in C++.

Assume a template class

template<class _T, int _n>
class Array{};

1) typedef Array Point;
No.

2) typedef Array<double, n> Point<n>;
No.

3) #define Point<n> Array<double, n>
No.

4) #define Point(n) Array<double, n>

Yes.

You seem to be looking for templated typedefs. Unfortunately, the current
version of C++ doesn't support those. One possible, but IMHO ugly
workaround would be to put your typedef into a templated dummy struct like:

template<int N>
struct Point
{
typedef Array<double, N> type;
};

//...

Point<3>::type p;
 

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

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top