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

Question on define.

P: n/a
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<nArray<double, n>

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

Thanks!

Shuisheng

Sep 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
shuisheng wrote:
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<nArray<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.

Sep 21 '06 #2

P: n/a
shuisheng wrote:
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<nArray<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, Ntype;
};

//...

Point<3>::type p;

Sep 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.