gborzi
March 19th, 2006, 08:31 PM
Maybe this question is quite dumb, but I'm new to AMD64 programming. I have the following C++ code:
template<typename T> class array {
public:
typedef T baseT;
typedef _array_iter< array<T> > iterT;
array() : n_(0), data_(0) {}
array( size_t n ) : n_(n) { data_ = new T[n_]; }
array( int n ) : n_(n) { data_ = new T[n_]; }
// array( unsigned int n ) : n_(n) { data_ = new T[n_]; }
array( unsigned short n ) : n_(n) { data_ = new T[n_]; }
array( unsigned char n ) : n_(n) { data_ = new T[n_]; }
.....
template<typename U> array( U rhs ) : n_(0), data_(0) {
rhs.assign(*this);
}
.....
};
on 32-bit architectures the constructors taking size_t and unsigned int are the same because size_t is actually an unsigned int on them. So I need to comment out one of the two.
But on 64-bit architectures size_t is not an unsigned int, so I need both constructors. For example, without the constructor with unsigned int with a code such as
unsigned int n = 10; array<double> v(n);
the compiler will try to apply the last constructor ( template<typename U> array( U rhs ) )
and will issue an error.
Is there some macro that identify the architecture, so that I can write the same code for both architectures, i.e. something like:
template<typename T> class array {
public:
typedef T baseT;
typedef _array_iter< array<T> > iterT;
array() : n_(0), data_(0) {}
array( size_t n ) : n_(n) { data_ = new T[n_]; }
array( int n ) : n_(n) { data_ = new T[n_]; }
#ifdef <macro for 64-bit>
array( unsigned int n ) : n_(n) { data_ = new T[n_]; }
#endif
array( unsigned short n ) : n_(n) { data_ = new T[n_]; }
array( unsigned char n ) : n_(n) { data_ = new T[n_]; }
.....
template<typename U> array( U rhs ) : n_(0), data_(0) {
rhs.assign(*this);
}
.....
};
I know I can modify the makefile so as to define a macro that does the trick, but I would like to have the same code and makefile. Moreover, I think that there should be some macro already defined.
Thanks in advance for your help.
template<typename T> class array {
public:
typedef T baseT;
typedef _array_iter< array<T> > iterT;
array() : n_(0), data_(0) {}
array( size_t n ) : n_(n) { data_ = new T[n_]; }
array( int n ) : n_(n) { data_ = new T[n_]; }
// array( unsigned int n ) : n_(n) { data_ = new T[n_]; }
array( unsigned short n ) : n_(n) { data_ = new T[n_]; }
array( unsigned char n ) : n_(n) { data_ = new T[n_]; }
.....
template<typename U> array( U rhs ) : n_(0), data_(0) {
rhs.assign(*this);
}
.....
};
on 32-bit architectures the constructors taking size_t and unsigned int are the same because size_t is actually an unsigned int on them. So I need to comment out one of the two.
But on 64-bit architectures size_t is not an unsigned int, so I need both constructors. For example, without the constructor with unsigned int with a code such as
unsigned int n = 10; array<double> v(n);
the compiler will try to apply the last constructor ( template<typename U> array( U rhs ) )
and will issue an error.
Is there some macro that identify the architecture, so that I can write the same code for both architectures, i.e. something like:
template<typename T> class array {
public:
typedef T baseT;
typedef _array_iter< array<T> > iterT;
array() : n_(0), data_(0) {}
array( size_t n ) : n_(n) { data_ = new T[n_]; }
array( int n ) : n_(n) { data_ = new T[n_]; }
#ifdef <macro for 64-bit>
array( unsigned int n ) : n_(n) { data_ = new T[n_]; }
#endif
array( unsigned short n ) : n_(n) { data_ = new T[n_]; }
array( unsigned char n ) : n_(n) { data_ = new T[n_]; }
.....
template<typename U> array( U rhs ) : n_(0), data_(0) {
rhs.assign(*this);
}
.....
};
I know I can modify the makefile so as to define a macro that does the trick, but I would like to have the same code and makefile. Moreover, I think that there should be some macro already defined.
Thanks in advance for your help.