Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10659 > unrolled thread
| Started by | softwareEngineer <ovNOSPAM@NOSPAMlibero.it> |
|---|---|
| First post | 2011-10-04 16:00 +0200 |
| Last post | 2011-10-07 10:42 +0200 |
| Articles | 12 — 7 participants |
Back to article view | Back to comp.lang.c++
template with STL container softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-04 16:00 +0200
Re: template with STL container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-04 10:01 -0400
Re: template with STL container softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-04 16:16 +0200
Re: template with STL container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-04 10:44 -0400
Re: template with STL container Noah Roberts <roberts.noah@gmail.com> - 2011-10-04 07:48 -0700
Re: template with STL container Juha Nieminen <nospam@thanks.invalid> - 2011-10-04 19:14 +0000
Re: template with STL container Nobody <nobody@nowhere.com> - 2011-10-05 09:51 +0100
Re: template with STL container Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-05 08:05 -0400
Re: template with STL container Juha Nieminen <nospam@thanks.invalid> - 2011-10-05 15:17 +0000
Re: template with STL container Gert-Jan de Vos <gert-jan.de.vos@onsneteindhoven.nl> - 2011-10-05 05:43 -0700
Re: template with STL container "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-10-05 20:08 +0100
Re: template with STL container softwareEngineer <ovNOSPAM@NOSPAMlibero.it> - 2011-10-07 10:42 +0200
| From | softwareEngineer <ovNOSPAM@NOSPAMlibero.it> |
|---|---|
| Date | 2011-10-04 16:00 +0200 |
| Subject | template with STL container |
| Message-ID | <j6f3eg$e4r$1@tdi.cu.mi.it> |
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.
[toc] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-10-04 10:01 -0400 |
| Message-ID | <j6f3kh$hd0$1@dont-email.me> |
| In reply to | #10659 |
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
[toc] | [prev] | [next] | [standalone]
| From | softwareEngineer <ovNOSPAM@NOSPAMlibero.it> |
|---|---|
| Date | 2011-10-04 16:16 +0200 |
| Message-ID | <4E8B1520.80102@NOSPAMlibero.it> |
| In reply to | #10660 |
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
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-10-04 10:44 -0400 |
| Message-ID | <j6f656$41g$1@dont-email.me> |
| In reply to | #10661 |
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
[toc] | [prev] | [next] | [standalone]
| From | Noah Roberts <roberts.noah@gmail.com> |
|---|---|
| Date | 2011-10-04 07:48 -0700 |
| Message-ID | <41f9c992-4fd5-450c-8e0f-e6ef018eb6bf@20g2000yqq.googlegroups.com> |
| In reply to | #10661 |
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
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-10-04 19:14 +0000 |
| Message-ID | <4e8b5b2d$0$3076$7b1e8fa0@news.nbl.fi> |
| In reply to | #10661 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-10-05 09:51 +0100 |
| Message-ID | <pan.2011.10.05.08.52.07.605000@nowhere.com> |
| In reply to | #10661 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-10-05 08:05 -0400 |
| Message-ID | <j6hh6c$6um$1@dont-email.me> |
| In reply to | #10676 |
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
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-10-05 15:17 +0000 |
| Message-ID | <4e8c74f8$0$2824$7b1e8fa0@news.nbl.fi> |
| In reply to | #10679 |
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").
[toc] | [prev] | [next] | [standalone]
| From | Gert-Jan de Vos <gert-jan.de.vos@onsneteindhoven.nl> |
|---|---|
| Date | 2011-10-05 05:43 -0700 |
| Message-ID | <f2c70454-6ee2-45e7-9312-f65abfeb4ce0@j19g2000vbn.googlegroups.com> |
| In reply to | #10676 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-10-05 20:08 +0100 |
| Message-ID | <j6i9v5$1tje$1@adenine.netfront.net> |
| In reply to | #10676 |
"Nobody" <nobody@nowhere.com> wrote in message
news:pan.2011.10.05.08.52.07.605000@nowhere.com...
> 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.
>
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.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | softwareEngineer <ovNOSPAM@NOSPAMlibero.it> |
|---|---|
| Date | 2011-10-07 10:42 +0200 |
| Message-ID | <4E8EBB59.1000507@NOSPAMlibero.it> |
| In reply to | #10686 |
Il 05/10/2011 21:08, Paul <pchrist ha scritto:
> "Nobody"<nobody@nowhere.com> wrote in message
> news:pan.2011.10.05.08.52.07.605000@nowhere.com...
>> 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.
>>
> 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.
>
>
>
> --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Yes,
it's exactly what I meant ...
bye.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web