template with STL container

  • Thread starter softwareEngineer
  • Start date
S

softwareEngineer

Hi all,
I'd like to manage a template in a STL vector container i.e. :

template <typename T>
class MY_Value
{
// ...
T GetValue ();

private :
T _val;
};

now in my main would :
std::vector <MY_Value*> v;
MyValue<int> iV1 = 1;
MyValue<int> iV2 = 2;
v[0] = &iV1;
v[1] = &iV2;

but i have had a problem because (obviously) MyValue it's a template .... ;

There is a workaround to manage a template in a standard container (vector for
example) ?



bye.
 
V

Victor Bazarov

I'd like to manage a template in a STL vector container i.e. :

template <typename T>
class MY_Value
{
// ...
T GetValue ();

private :
T _val;
};

now in my main would :
std::vector <MY_Value*> v;

In order for this to compile, 'v' has to have a concrete type.
Otherwise, how should the compiler know how much memory to allocate for
it and how to initialize it?
MyValue<int> iV1 = 1;
MyValue<int> iV2 = 2;
v[0] = &iV1;
v[1] = &iV2;

but i have had a problem because (obviously) MyValue it's a template .... ;

There is a workaround to manage a template in a standard container
(vector for example) ?

Well, is there a reason that

std::vector<MY_Value<int>*> v;

does not work for you?

V
 
S

softwareEngineer

Il 04/10/2011 16:01, Victor Bazarov ha scritto:
I'd like to manage a template in a STL vector container i.e. :

template <typename T>
class MY_Value
{
// ...
T GetValue ();

private :
T _val;
};

now in my main would :
std::vector <MY_Value*> v;

In order for this to compile, 'v' has to have a concrete type. Otherwise, how
should the compiler know how much memory to allocate for it and how to
initialize it?
MyValue<int> iV1 = 1;
MyValue<int> iV2 = 2;
v[0] = &iV1;
v[1] = &iV2;

but i have had a problem because (obviously) MyValue it's a template .... ;

There is a workaround to manage a template in a standard container
(vector for example) ?

Well, is there a reason that

std::vector<MY_Value<int>*> v;

does not work for you?

V
Yes, but I need a vector of generic value (float, int, ecc.)

MyValue<int> iV1 = 1;
MyValue<float> iV2 = 2.1;
v[0] = &iV1;
v[1] = &iV2;

_ _
Ono
 
V

Victor Bazarov

Il 04/10/2011 16:01, Victor Bazarov ha scritto:
I'd like to manage a template in a STL vector container i.e. :

template <typename T>
class MY_Value
{
// ...
T GetValue ();

private :
T _val;
};

now in my main would :
std::vector <MY_Value*> v;

In order for this to compile, 'v' has to have a concrete type.
Otherwise, how should the compiler know how much memory to allocate
for it and how to initialize it?
MyValue<int> iV1 = 1;
MyValue<int> iV2 = 2;
v[0] = &iV1;
v[1] = &iV2;

but i have had a problem because (obviously) MyValue it's a template
.... ;

There is a workaround to manage a template in a standard container
(vector for example) ?

Well, is there a reason that

std::vector<MY_Value<int>*> v;

does not work for you?

V
Yes, but I need a vector of generic value (float, int, ecc.)

You didn't say that before, did you?
MyValue<int> iV1 = 1;
MyValue<float> iV2 = 2.1;
v[0] = &iV1;
v[1] = &iV2;

Check the FAQ section 34.

V
 
N

Noah Roberts

Il 04/10/2011 16:01, Victor Bazarov ha scritto:






In order for this to compile, 'v' has to have a concrete type. Otherwise, how
should the compiler know how much memory to allocate for it and how to
initialize it?
MyValue<int> iV1 = 1;
MyValue<int> iV2 = 2;
v[0] = &iV1;
v[1] = &iV2;
but i have had a problem because (obviously) MyValue it's a template ..... ;
There is a workaround to manage a template in a standard container
(vector for example) ?
Well, is there a reason that
    std::vector<MY_Value<int>*> v;
does not work for you?

Yes, but I need a vector of generic value (float, int, ecc.)

MyValue<int> iV1 = 1;
MyValue<float> iV2 = 2.1;
v[0] = &iV1;
v[1] = &iV2;

Can't be done. You have to either inherit from something higher and
downcast, store void* (not recommended), or use something like
boost.any
 
J

Juha Nieminen

softwareEngineer said:
MyValue<int> iV1 = 1;
MyValue<float> iV2 = 2.1;

MyValue<int> and MyValue<float> are completely different and independent
types that have nothing to do with each other (except for the fact that
these types were "autogenerated" from one common template). Hence all
restrictions apply to them as to any other (non-templated) distinct types.
 
N

Nobody

Yes, but I need a vector of generic value (float, int, ecc.)

In which case, the values need a common base class, e.g.:

class MY_Value {
...
};

template <typename T>
class MY_Value_T : public MY_Value {
...
};

std::vector<MY_Value*> v;

MyValue_T<int> iV1 = 1;
MyValue_T<float> iV2 = 2.1;
v[0] = static_cast<MY_Value*>(&iV1);
v[1] = static_cast<MY_Value*>(&iV2);

Casting the vector elements back to their correct type is a bit more
involved, as you need to know what their correct type is, either by
using RTTI or by explicitly adding a tag field to the base class.
 
V

Victor Bazarov

Yes, but I need a vector of generic value (float, int, ecc.)

In which case, the values need a common base class, e.g.:

class MY_Value {
...
};

template<typename T>
class MY_Value_T : public MY_Value {
...
};

std::vector<MY_Value*> v;

MyValue_T<int> iV1 = 1;
MyValue_T<float> iV2 = 2.1;
v[0] = static_cast<MY_Value*>(&iV1);
v[1] = static_cast<MY_Value*>(&iV2);

There is no need for 'static_cast', the conversion from a pointer to
derived to a pointer to base is implicit.
Casting the vector elements back to their correct type is a bit more
involved, as you need to know what their correct type is, either by
using RTTI or by explicitly adding a tag field to the base class.

V
 
G

Gert-Jan de Vos

Yes, but I need a vector of generic value (float, int, ecc.)

In which case, the values need a common base class, e.g.:

        class MY_Value {
        ...
        };

        template <typename T>
        class MY_Value_T : public MY_Value {
        ...
        };

        std::vector<MY_Value*> v;

        MyValue_T<int> iV1 = 1;
        MyValue_T<float> iV2 = 2.1;
        v[0] = static_cast<MY_Value*>(&iV1);
        v[1] = static_cast<MY_Value*>(&iV2);

Casting the vector elements back to their correct type is a bit more
involved, as you need to know what their correct type is, either by
using RTTI or by explicitly adding a tag field to the base class.

If you need this, use boost::any. It's close to this, but exposes
an interface based on value semantics.
 
J

Juha Nieminen

Victor Bazarov said:
v[0] = static_cast<MY_Value*>(&iV1);
v[1] = static_cast<MY_Value*>(&iV2);

There is no need for 'static_cast', the conversion from a pointer to
derived to a pointer to base is implicit.

Which, btw, isn't simply an arbitrary rule, but actually one of the most
basic tenets of OOP (strongly related to the "is-a rule").
 
S

softwareEngineer

Il 05/10/2011 21:08, Paul <pchrist ha scritto:
Nobody said:
Yes, but I need a vector of generic value (float, int, ecc.)
In which case, the values need a common base class, e.g.:

class MY_Value {
...
};

template<typename T>
class MY_Value_T : public MY_Value {
...
};

std::vector<MY_Value*> v;

MyValue_T<int> iV1 = 1;
MyValue_T<float> iV2 = 2.1;
v[0] = static_cast<MY_Value*>(&iV1);
v[1] = static_cast<MY_Value*>(&iV2);

Casting the vector elements back to their correct type is a bit more
involved, as you need to know what their correct type is, either by
using RTTI or by explicitly adding a tag field to the base class.
You can do this kind of thing if you have a limited set of different types:

template<class T1=float, class T2=int, class T3=std::string>
class Base{
public:
virtual operator T1(){return 0;}
virtual operator T2(){return 0;}
virtual operator T3(){return 0;}
};

template<class T, class T1=float, class T2=int, class T3=std::string>
class Derived:public Base<T1,T2,T3>{
T itsData;
public:
Derived(T x):itsData(x){};
virtual operator T(){return itsData;}
};

Derived<float> d1(-4.0/3.0);
Derived<std::string> d2("Hello");

std::vector< Base<>*> v1;
v1.push_back(&d1);
float f1 = *v1[0];
v1[0] =&d2;
std::string s1= *v1[0];

Its more difficult if you want unlimited amount of different types.
Yes,
it's exactly what I meant ...

bye.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top