template class in STL container

J

Johan

Hi,

Hi like to create a linked list with a template class ( see below ). How to
make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;

template <class T>class DataVariant
{
public :
DataVariant(T value);

T getValue();

private :
T value;
};

template <class T>DataVariant<T> :: DataVariant(T value)
: value(value)
{
}

template <class T>T DataVariant<T> :: getValue()
{
return value;
}


regards Johan
 
J

John Harrison

Johan said:
Hi,

Hi like to create a linked list with a template class ( see below ). How to
make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;

list< DataVariant<int> > DataList1;
list< DataVariant<string> > DataList2;

The only thing to watch out for is that you must put a space between the
first > and the second >.

john
 
D

David Hilsee

Johan said:
Hi,

Hi like to create a linked list with a template class ( see below ). How to
make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;

template <class T>class DataVariant
{
public :
DataVariant(T value);

T getValue();

private :
T value;
};

template <class T>DataVariant<T> :: DataVariant(T value)
: value(value)
{
}

template <class T>T DataVariant<T> :: getValue()
{
return value;
}

If you want to store objects of different types in the same container, see
the FAQ (http://www.parashift.com/c++-faq-lite) section 34 ("Container
classes and templates"), question 4 "How can I build a <favorite container>
of objects of different types?"). If you choose to go with the "second
case" (no common base) approach in the FAQ, then boost::any
(http://www.boost.org/doc/html/any.html) may be useful.
 
S

Sachin Garg

John Harrison said:
list< DataVariant<int> > DataList1;
list< DataVariant<string> > DataList2;

The only thing to watch out for is that you must put a space between the
first > and the second >.

john

I thought this 'space' problem was only in my VC compiler. Or is it
true across others like gcc too? Is there a specific reason why this
issue cant be removed from compilers?

Sachin Garg [India]
 
J

John Harrison

Sachin Garg said:
"John Harrison" <[email protected]> wrote in message
How
to

list< DataVariant<int> > DataList1;
list< DataVariant<string> > DataList2;

The only thing to watch out for is that you must put a space between the
first > and the second >.

john

I thought this 'space' problem was only in my VC compiler. Or is it
true across others like gcc too? Is there a specific reason why this
issue cant be removed from compilers?

Sachin Garg [India]

It's true on all compilers. The rules of C++ say that >> is the right shift
operator, so you must put in a space.

These rules may change in the future, and no doubt some compilers are smart
enough to figure out what you really meant, but as it stands at the moment a
space is required.

john
 
G

Greg Comeau

It's true on all compilers. The rules of C++ say that >> is the right shift
operator, so you must put in a space.

These rules may change in the future, and no doubt some compilers are smart
enough to figure out what you really meant, but as it stands at the moment a
space is required.

Comeau C++'s current error on something like that is:

error: space required between adjacent ">" delimiters of
nested template argument lists (">>" is the right shift operator)

so it's able to figure out some of the cases, and eventually probably
will accept it (silently or with a warning, depending upon the mode
requested, and whether this is resolved in some manner in C++0x).
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top