473,854 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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::s tring, option_type > optmap;

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

template<typena me T > void set_value(std:: string const &id,
T value) { optmap[id] = value; }
template<typena me 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<voi d * > but I'd *really* like to avoid doing
that if possible.

Any comments, suggestions would be very appreciated.

Cheers
Jul 22 '05 #1
1 1856
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 "heterogene ous 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<typena me 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::s tring, option_type > optmap;

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

template<typena me T > void set_value(std:: string const &id,
T value) { optmap[id] = value; }
template<typena me 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<voi d * > 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_typ e" 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
3755
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 ============================= #include <vector> #include <list> #include <set> #include <functional>
5
3040
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, and maps? I know that the word "iterator" is typedef'd from something for everyone of these containers so I was curious if all these functions are valid. On the same note, should I use "find_if" or "remove_if" for the following
6
2062
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 constructors) which have this base class: template<class T>
2
1560
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 specific C1 member function. Both containers contains the same number of elements (or at least, C1 is larger than C2). As suggested by S. Meyers (and others), I would like to use an STL algorithm instead of a custom loop. Let say I have the...
43
5046
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 libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
13
2558
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;} }; int main() { int arr = {1, 2, 3}; set<int,Asc> asc(arr, arr+3); set<int,Des>::iterator beg = asc.begin(); // set<int,Des>::iterator end = asc.end(); // copy(beg, end, ostream_iterator<int>(cout,...
13
2032
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 "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
7
2074
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 that matches this requirement, the only constructors are: public: // allocation/deallocation _Rb_tree() { }
0
1194
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 record I used datalist. And becoz of query return different no of column based on condtion I created template class for design and itembinding at run time . I manage to create template column() but confuse over how to bind data. As no of column...
0
9899
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10672
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10740
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10362
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7909
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.