Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template with STL container (http://www.velocityreviews.com/forums/t754621-template-with-stl-container.html)

softwareEngineer 10-04-2011 02:00 PM

template with STL container
 
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.


Victor Bazarov 10-04-2011 02:01 PM

Re: template with STL container
 
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

softwareEngineer 10-04-2011 02:16 PM

Re: template with STL container
 
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

Victor Bazarov 10-04-2011 02:44 PM

Re: template with STL container
 
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

Noah Roberts 10-04-2011 02:48 PM

Re: template with STL container
 
On Oct 4, 7:16*am, softwareEngineer <ovNOS...@NOSPAMlibero.it> 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

Juha Nieminen 10-04-2011 07:14 PM

Re: template with STL container
 
softwareEngineer <ovNOSPAM@nospamlibero.it> 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.

Nobody 10-05-2011 08:51 AM

Re: template with STL container
 
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.


Victor Bazarov 10-05-2011 12:05 PM

Re: template with STL container
 
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

Gert-Jan de Vos 10-05-2011 12:43 PM

Re: template with STL container
 
On Oct 5, 10:51*am, Nobody <nob...@nowhere.com> 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.

Juha Nieminen 10-05-2011 03:17 PM

Re: template with STL container
 
Victor Bazarov <v.bazarov@comcast.invalid> 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").


All times are GMT. The time now is 02:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.