Workaround for number of template args?

J

Joseph Turian

Okay, bear with me for a second here. This is a general C++ question.

In g++ 3.2.3, I can control the allocator as follows:
vector<int, __single_client_alloc> v;

In g++ 3.4, however, they removed this functionality, so I must do:
vector<int> v;


Now, to make the code portable, I could have something like this:
#ifdef SINGLE_CLIENT_ALLOC
vector<int, __single_client_alloc> v;
#else
vector<int> v;
#endif

where SINGLE_CLIENT_ALLOC is determined by a configuration script.

However, this solution is undesirable because I have to replace many
single line declarations with five lines.

Can I define a macro SCA, s.t. 'SCA(vector<int>) v' that would have the
desired effect?
If not, is there a single-line solution to choose between using the two
template parameters or not?

Joseph
 
V

Victor Bazarov

Joseph said:
Okay, bear with me for a second here. This is a general C++ question.

In g++ 3.2.3, I can control the allocator as follows:
vector<int, __single_client_alloc> v;

In g++ 3.4, however, they removed this functionality, so I must do:
vector<int> v;


Now, to make the code portable, I could have something like this:
#ifdef SINGLE_CLIENT_ALLOC
vector<int, __single_client_alloc> v;
#else
vector<int> v;
#endif

where SINGLE_CLIENT_ALLOC is determined by a configuration script.

However, this solution is undesirable because I have to replace many
single line declarations with five lines.

Can I define a macro SCA, s.t. 'SCA(vector<int>) v' that would have
the desired effect?
If not, is there a single-line solution to choose between using the
two template parameters or not?

#ifdef SINGLE_CLIENT_ALLOC
#define DEF_VECTOR(what) vector<what, __single_client_alloc>
#else
#define DEF_VECTOR(what) vector<what>
#endif

....

DEF_VECTOR(int) v;

HTH
 
J

Joseph Turian

Victor said:
#ifdef SINGLE_CLIENT_ALLOC
#define DEF_VECTOR(what) vector<what, __single_client_alloc>
#else
#define DEF_VECTOR(what) vector<what>
#endif

...

DEF_VECTOR(int) v;


Ah, but I also want to be able to vary the template type, i.e. deque
instead of vector.

In that case, is there a more elegant option than making a #define for
every type I use?

Joseph
 
V

Victor Bazarov

Joseph said:
Ah, but I also want to be able to vary the template type, i.e. deque
instead of vector.

In that case, is there a more elegant option than making a #define for
every type I use?

:)

You could do

#ifdef SINGLE_CLIENT_ALLOC
# define DEF_CONTAINER(which, ofwhat) which<ofwhat,
__single_client_alloc>
...

DEF_CONTAINER(deque, int) di;
DEF_CONTAINER(vector, double) vd;

or do away without an additiona define altogether:

template<class T> struct use_vector {
#ifdef SINGLE_CLIENT_ALLOC
typedef vector<T, __single_client_alloc> _;
#endif
typedef vector<T> _;
#endif
};

..
use_vector<int>::_ v; // a bit cleaner, probably

template<class T> struct use_deque {
#ifdef SINGLE_CLIENT_ALLOC
typedef deque<T, __single_client_alloc> _;
#endif
typedef deque<T> _;
#endif
};

use_deque<double>::_ d; // but more to type...

V
 
J

Joseph Turian

Victor said:
or do away without an additiona define altogether:

template<class T> struct use_vector {
#ifdef SINGLE_CLIENT_ALLOC
typedef vector<T, __single_client_alloc> _;
#endif
typedef vector<T> _;
#endif
};


I guess that makes the most sense, since the #define only works with
containers that have exactly one template parameter.

Thanks!

Joseph
 
V

Victor Bazarov

Joseph said:
Victor Bazarov wrote:





I guess that makes the most sense, since the #define only works with
containers that have exactly one template parameter.

I don't think there is much difference. The other template arguments
have default values and that's being utilised in both situations. The
#define only substitutes strings. The only difference I like is that
in the case of #define you supply arguments in parentheses, whereas in
the case of a 'use_blah' struct template you supply them in the angle
brackets -- easier to recognise as a template...

V
 
O

Old Wolf

Joseph said:
#ifdef SINGLE_CLIENT_ALLOC
vector<int, __single_client_alloc> v;
#else
vector<int> v;
#endif

where SINGLE_CLIENT_ALLOC is determined by a configuration script.

However, this solution is undesirable because I have to replace many
single line declarations with five lines.

Can I define a macro SCA, s.t. 'SCA(vector<int>) v' that would have the
desired effect?

#ifdef SINGLE_CLIENT_ALLOC
# define SCA , __single_client_alloc
#else
# define SCA
#endif

vector<int SCA> v;
deque<long SCA> d;
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top