473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Workaround for number of template args?

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
6 2170
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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ganny | last post by:
Is it possible to write a template function min that takes variable number of arguments? It should be without using ugly C var args, arrays, or statically overloading the method for N arguments....
4
by: Andy | last post by:
I would like to build a website using a template. I got one from internet but after I open it my new Internet Explorer gives me a warning : 'To help protect your security, Internet Explorer has...
2
by: David Zack | last post by:
Hello. I'm currently working on a tool for an intranet that allows a user to launch a Word document template from an HTML page on a corporate portal. I would now like to run a particular macro...
3
by: Steve | last post by:
Is there any way of specifying the startMode when using the xslTransform class? We are updating code which used msxml to the system.xml classes but can find no way to specify the startMode. We...
2
by: PIEBALD | last post by:
OK, here's my latest workaround for the lack of inherited constructors... It requires that the class have a parameterless (default) constructor and a public virtual "Initialize" method (which...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
0
by: ajay.padala | last post by:
Hi I would like to be able to get the inputs that are needed into a template file programmatically. For example if we had a template: ===================== $name has $num marbles in his...
9
by: Rob | last post by:
I want to do something like this: template <typename MyType> std::vector<MYTypeget(SomeClass c) { ...elided... } int main()
0
by: Robin Becker | last post by:
Joe Strout wrote: ........ you could use something like this to record the lookups .... def __new__(cls,*args,**kwds): .... self = dict.__new__(cls,*args,**kwds) .... self.__record =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.