473,405 Members | 2,338 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,405 software developers and data experts.

String Parameters to Template Classes

Is there a way to pass a string parameter to a template class? I would
like to do something like:

MyClass<int, "blah", 5> test;
Thanks

Oct 4 '05 #1
7 1856
bsmatt wrote:
Is there a way to pass a string parameter to a template class? I would
like to do something like:

MyClass<int, "blah", 5> test;


No. Non-type arguments need to be either integral constants, or addresses
of (or references to) objects with external linkage. String literals are
neither.

V
Oct 4 '05 #2
Thanks for the quick response.

That is what I was afraid of.

The reason I am asking is that I am working on a simple way to track
memory allocations so that I can get a list of memory leaks. I am using
STL and those allocations are done in a way that I can track but I
can't figure out how to get information about where the allocation came
from. For example:

std::vector<int> test;
test.push_back(5);

will allocate memory. In other situations I am using __FILE__ and
__LINE__ so that I can see exactly where a "new" happens. I would like
to get enough info to a least know what STL object the memory is
allocated for (in my example I would like to know that the allocation
was associated with the definition of test). Got any suggestions on how
I can accomplish this?

Thanks

Oct 4 '05 #3
bsmatt wrote:
The reason I am asking is that I am working on a simple way to track
memory allocations so that I can get a list of memory leaks. I am using
STL and those allocations are done in a way that I can track but I
can't figure out how to get information about where the allocation came
from. For example:

std::vector<int> test;
test.push_back(5);

will allocate memory. In other situations I am using __FILE__ and
__LINE__ so that I can see exactly where a "new" happens. I would like
to get enough info to a least know what STL object the memory is
allocated for (in my example I would like to know that the allocation
was associated with the definition of test). Got any suggestions on how
I can accomplish this?


Not really. 'push_back(5)' will cause allocation (iff it needs to resize
the vector), but that allocation will happen where 'push_back' is
implemented, not where you call it. In order to be able to track
'push_back' calls, you will need to reimplement (by wrapping, for example)
the whole Standard Library (to track all containers your program may be
using).

V
Oct 4 '05 #4
On Tue, 04 Oct 2005 09:02:17 -0700, bsmatt wrote:

<snip>
In other situations I am using __FILE__ and
__LINE__ so that I can see exactly where a "new" happens. I would like
to get enough info to a least know what STL object the memory is
allocated for (in my example I would like to know that the allocation
was associated with the definition of test). Got any suggestions on how
I can accomplish this?


How about instead of:

MyClass<int, "blah", 5> test;

you use:

MyClass<int, 5> test("blah");

or even:

MyClass<int> test("blah", 5);

and write the appropriate constructor.

Is there a reason to have a separate type for each instance where it's
used?

- Jay

Oct 4 '05 #5
My idea was to create a new allocator that kept track of where the
object was created so that when allocating memory I would know where
the object was defined. It would look something like

std::vector<int, MyAlloc<int, __FILE__, __LINE__>> test;

This way the allocator knows where it came from...but that won't work.

It looks like the solution I am going to go with is a wrapper based
approach where I can push and pop allocation location info into my
memory tracker before making the calls into the STL.

Oct 4 '05 #6
On Tue, 04 Oct 2005 11:03:13 -0700, bsmatt wrote:
My idea was to create a new allocator that kept track of where the
object was created so that when allocating memory I would know where
the object was defined. It would look something like

std::vector<int, MyAlloc<int, __FILE__, __LINE__>> test;

This way the allocator knows where it came from...but that won't work.

It looks like the solution I am going to go with is a wrapper based
approach where I can push and pop allocation location info into my
memory tracker before making the calls into the STL.


I guess my question remains - why does each allocator have to have a
unique *type* (a different instantiation of the template to
effectively generate a new class) as opposed to member variables that hold
the __FILE__ and __LINE__ values in different objects?

- Jay
Oct 4 '05 #7
On Tue, 04 Oct 2005 20:04:55 +0000, Jay Nabonne wrote:

Never mind. I think I misread that.

Oct 4 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: arch | last post by:
Hi, I am trying to implement a class hierarchy where there will be a base class defining the interface and some common operations (interface+implentation): Constraints : no STL. This will be...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
5
by: ian | last post by:
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++ Design by Andrei Alexandrescu. The test code is listed below and the compiler error message that is generated is: ...
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.