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

Workaround for number of template args?

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

Aug 2 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Joseph Turian wrote:
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
Aug 2 '05 #2

P: n/a

Victor Bazarov wrote:
#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

Aug 2 '05 #3

P: n/a
Joseph Turian wrote:
Victor Bazarov wrote:
#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?


:-)

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
Aug 2 '05 #4

P: n/a

Victor Bazarov wrote:
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

Aug 2 '05 #5

P: n/a
Joseph Turian wrote:
Victor Bazarov wrote:

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.


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
Aug 2 '05 #6

P: n/a
Joseph Turian wrote:

#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;

Aug 3 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.