template with STL container

Discussion in 'C++' started by softwareEngineer, Oct 4, 2011.

  1. 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.
    softwareEngineer, Oct 4, 2011
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Oct 4, 2011
    #2
    1. Advertising

  3. 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
    softwareEngineer, Oct 4, 2011
    #3
  4. 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
    Victor Bazarov, Oct 4, 2011
    #4
  5. softwareEngineer

    Noah Roberts Guest

    On Oct 4, 7: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.)
    >
    > 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
    Noah Roberts, Oct 4, 2011
    #5
  6. softwareEngineer <> 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.
    Juha Nieminen, Oct 4, 2011
    #6
  7. softwareEngineer

    Nobody Guest

    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.
    Nobody, Oct 5, 2011
    #7
  8. 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
    Victor Bazarov, Oct 5, 2011
    #8
  9. On Oct 5, 10: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);
    >
    > 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.
    Gert-Jan de Vos, Oct 5, 2011
    #9
  10. Victor Bazarov <> 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").
    Juha Nieminen, Oct 5, 2011
    #10
  11. Il 05/10/2011 21:08, Paul <pchrist ha scritto:
    > "Nobody"<> wrote in message
    > news:p...
    >> 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 ---

    Yes,
    it's exactly what I meant ...

    bye.
    softwareEngineer, Oct 7, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maitre Bart
    Replies:
    2
    Views:
    510
    Maitre Bart
    Feb 11, 2004
  2. Johan
    Replies:
    5
    Views:
    3,461
    Greg Comeau
    Sep 23, 2004
  3. Replies:
    4
    Views:
    780
    Daniel T.
    Feb 16, 2006
  4. wolverine
    Replies:
    2
    Views:
    437
    Marcus Kwok
    Jul 24, 2006
  5. Saeed Amrollahi
    Replies:
    1
    Views:
    389
    Juha Nieminen
    Jun 18, 2009
Loading...

Share This Page