Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template with STL container

Reply
Thread Tools

template with STL container

 
 
softwareEngineer
Guest
Posts: n/a
 
      10-04-2011
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-04-2011
On 10/4/2011 10:00 AM, softwareEngineer wrote:
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
softwareEngineer
Guest
Posts: n/a
 
      10-04-2011
Il 04/10/2011 16:01, Victor Bazarov ha scritto:
> On 10/4/2011 10:00 AM, softwareEngineer wrote:
>> 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-04-2011
On 10/4/2011 10:16 AM, softwareEngineer wrote:
> Il 04/10/2011 16:01, Victor Bazarov ha scritto:
>> On 10/4/2011 10:00 AM, softwareEngineer wrote:
>>> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      10-04-2011
On Oct 4, 7:16*am, softwareEngineer <(E-Mail Removed)> wrote:
> Il 04/10/2011 16:01, Victor Bazarov ha scritto:
>
>
>
>
>
>
>
> > On 10/4/2011 10:00 AM, softwareEngineer wrote:
> >> 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;
>


Can't be done. You have to either inherit from something higher and
downcast, store void* (not recommended), or use something like
boost.any
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-04-2011
softwareEngineer <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      10-05-2011
On Tue, 04 Oct 2011 16:16:00 +0200, softwareEngineer wrote:

> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-05-2011
On 10/5/2011 4:51 AM, Nobody wrote:
> On Tue, 04 Oct 2011 16:16:00 +0200, softwareEngineer wrote:
>
>> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      10-05-2011
On Oct 5, 10:51*am, Nobody <(E-Mail Removed)> wrote:
> On Tue, 04 Oct 2011 16:16:00 +0200, softwareEngineer wrote:
> > 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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-05-2011
Victor Bazarov <(E-Mail Removed)> wrote:
>> 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").
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: STL container iterator not available within template? Saeed Amrollahi C++ 1 06-18-2009 09:37 PM
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
template class in STL container Johan C++ 5 09-23-2004 08:17 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM



Advertisments