Creating a standard container with a template class as a parameter

Discussion in 'C++' started by Aaron Walker, Dec 23, 2004.

  1. Aaron Walker

    Aaron Walker Guest

    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
    Aaron Walker, Dec 23, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 23, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Winter
    Replies:
    14
    Views:
    575
    Marijn
    Sep 18, 2003
  2. Calvin Lai
    Replies:
    1
    Views:
    315
    Cy Edmunds
    Dec 24, 2003
  3. christopher diggins
    Replies:
    16
    Views:
    728
    Pete Becker
    May 4, 2005
  4. Stuart Redmann
    Replies:
    5
    Views:
    465
    Stuart Redmann
    Dec 14, 2007
  5. A L
    Replies:
    1
    Views:
    499
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page