473,322 Members | 1,480 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,322 software developers and data experts.

Creating a standard container with a template class as a parameter

Hey folks,

I've got some code for an options class that stores user run time options (as
static members) so that they are availably any time an instance is created;
currently the code has a set_OPTION get_OPTION kind of function interface, but
as you can imagine that can get quite large after a while.

I've never messed with templates before, but this seemed like a good
opportunity to learn. The code (that doesn't compile) is below, but my main
question is in regards to the std::map I create (arg 2). I'd like the map to
be able to hold any kind of option_type, but options_T cannot be a template
(otherwise it'd hold all option_type<T>'s, not any kind).

<code>
template<typename T >
class option_type
{
public:
T value;
option_type& operator=(const T &v)
{ value = v; return *this; }
};

class options_T
{
protected:
static std::map<std::string, option_type > optmap;

static option_type<bool > _verbose;
static option_type<std::string > _user;
/* more opts of different types */
public:
options_T();

template<typename T > void set_value(std::string const &id,
T value) { optmap[id] = value; }
template<typename T > T get_value(std::string const &id)
{ return optmap[id].value; }
};
</code>

default option values are set in the source file, and the constructor is solely
for inserting the id's/option_type's into optmap.

Now, I know I cannot do it the way it is above, but is there anyway to specify
optmap as being able to hold option_type of any type? The only thing I can
think of is using option_type<void * > but I'd *really* like to avoid doing
that if possible.

Any comments, suggestions would be very appreciated.

Cheers
Jul 22 '05 #1
1 1833
Aaron Walker wrote:
[...] The code (that doesn't compile) is below, but my
main question is in regards to the std::map I create (arg 2). I'd like
the map to be able to hold any kind of option_type, but options_T cannot
be a template (otherwise it'd hold all option_type<T>'s, not any kind).
You can't want a container _specified_ to hold objects of a certain type
to hold a figment of your imagination (that's what templates are).

Read the archives about "heterogeneous container", but know this: if some
template is defined to take a _type_ as one of its arguments, you will
_have_to_ give it a _type_, and not a _template_. Containers hold objects
and not something that designates "a possibility to become a type". In
order for your container to hold objects, you need to define what _type_
those objects have. That's all.
<code>
template<typename T >
class option_type
{
public:
T value;
option_type& operator=(const T &v)
{ value = v; return *this; }
};

class options_T
{
protected:
static std::map<std::string, option_type > optmap;

static option_type<bool > _verbose;
static option_type<std::string > _user;
/* more opts of different types */
public:
options_T();

template<typename T > void set_value(std::string const &id,
T value) { optmap[id] = value; }
template<typename T > T get_value(std::string const &id)
{ return optmap[id].value; }
};
</code>

default option values are set in the source file, and the constructor is
solely for inserting the id's/option_type's into optmap.

Now, I know I cannot do it the way it is above, but is there anyway to
specify optmap as being able to hold option_type of any type?
No.
The only
thing I can think of is using option_type<void * > but I'd *really* like
to avoid doing that if possible.


Not possible, most likely. Unless, of course, you create a base class for
all your "option_type" things (whatever meaning you give to that), and
then store pointers to that base class in your map. Read the archives,
you're not the first one to come up with the idea of "a container that
stores anything I want".

V
Jul 22 '05 #2

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

Similar topics

0
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
5
by: sks_cpp | last post by:
Are the standard library functions pertinent to both sequence containers and associative containers? For example, if "find_if", "remove_if", etc... valid for both lists, deques, vectors, sets,...
6
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual...
2
by: Maitre Bart | last post by:
What I want to perform is calling a member function of container 1 (CRunner), using as argument the value of a container 2 (CNames). The purpose is to transfer values from C2 into C1 using a...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
7
by: desktop | last post by:
I the C++ standard page 472 it says that an associative container can be constructed like X(i,j,c) where i and j are input iterators to elements. But in the implementation there is no constructor...
0
by: imranabdulaziz | last post by:
Hi all, I am using asp.net and C# Visual studio 2005. Let me explain the scenario. I have stored procedure which return very no of column based on condition. Becoz I have to show columnwise...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.