Creating a standard container with a template class as a parameter

A

Aaron Walker

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
 
V

Victor Bazarov

Aaron said:
[...] 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
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top