Different tuples to one container? (One type of a pointer to point to different kinds of tuples?)

Discussion in 'C++' started by fff_afafaf@yahoo.com, Oct 4, 2006.

  1. Guest

    Do you know is it possible to put different kinds of tuples to one
    container?
    E.g. to a vector?
    (The lengths of the tuples are different, and also the types in the
    tuples are different..
    -> Is it possible to make a pointer, which can point to all of these
    tuples?)


    #include <tr1/tuple>
    #include <iostream>
    #include <string>
    using std::tr1::tuple; using std::tr1::get;
    using std::cout;

    int main()
    {
    // Just any kinds of tuples:

    tuple<int,int> t0(1, 2);
    tuple<int,int, int> t1(1, 2, 3);
    tuple<int,long> t2(1, 2);
    tuple<int,std::string> t3(1, "2str");
    tuple<int,std::string, int> t4(1, "2string", 3);
    tuple<int,int> t5(1, 2);

    // -> Is it possible to store those e.g. to a vector?

    return 0;
    }
     
    , Oct 4, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Do you know is it possible to put different kinds of tuples to one
    > container?
    > E.g. to a vector?
    > (The lengths of the tuples are different, and also the types in the
    > tuples are different..
    > -> Is it possible to make a pointer, which can point to all of these
    > tuples?)
    >
    >
    > #include <tr1/tuple>
    > #include <iostream>
    > #include <string>
    > using std::tr1::tuple; using std::tr1::get;
    > using std::cout;
    >
    > int main()
    > {
    > // Just any kinds of tuples:
    >
    > tuple<int,int> t0(1, 2);
    > tuple<int,int, int> t1(1, 2, 3);
    > tuple<int,long> t2(1, 2);
    > tuple<int,std::string> t3(1, "2str");
    > tuple<int,std::string, int> t4(1, "2string", 3);
    > tuple<int,int> t5(1, 2);
    >
    > // -> Is it possible to store those e.g. to a vector?
    >
    > return 0;
    > }


    When you instantiate a class template with particular parameters, that
    creates an actual class, i.e., a type, and as you likely know, you
    can't have a (non-void) pointer to disparate classes unless they are
    related by inheritance (TR1 tuples are not). You could, however, use a
    wrapper such as boost::any to put heterogeneous types into a
    homogeneous container such as std::vector (see
    http://boost.org/doc/html/any.html).

    Cheers! --M
     
    mlimber, Oct 4, 2006
    #2
    1. Advertising

  3. Pete Becker Guest

    Re: Different tuples to one container? (One type of a pointer topoint to different kinds of tuples?)

    wrote:
    > Do you know is it possible to put different kinds of tuples to one
    > container?
    > E.g. to a vector?
    > (The lengths of the tuples are different, and also the types in the
    > tuples are different..
    > -> Is it possible to make a pointer, which can point to all of these
    > tuples?)
    >


    void* will do it.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Oct 4, 2006
    #3
  4. On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <>
    wrote:
    > wrote:
    >> Do you know is it possible to put different kinds of tuples to one
    >> container?
    >> E.g. to a vector?

    >
    >void* will do it.


    A little OO will do it even better!
     
    Roland Pibinger, Oct 4, 2006
    #4
  5. Pete Becker Guest

    Re: Different tuples to one container? (One type of a pointer topoint to different kinds of tuples?)

    Roland Pibinger wrote:
    > On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <>
    > wrote:
    >> wrote:
    >>> Do you know is it possible to put different kinds of tuples to one
    >>> container?
    >>> E.g. to a vector?

    >> void* will do it.

    >
    > A little OO will do it even better!


    You misrepresented what I said by snipping the following, which is what
    I replied to:

    > -> Is it possible to make a pointer, which can point to all of these
    > tuples?)


    Plonk.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
     
    Pete Becker, Oct 4, 2006
    #5
  6. Re: Different tuples to one container? (One type of a pointer topoint to different kinds of tuples?)

    Pete Becker schrieb:
    > Roland Pibinger wrote:
    >> On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <>
    >> wrote:
    >>> wrote:
    >>>> Do you know is it possible to put different kinds of tuples to one
    >>>> container?
    >>>> E.g. to a vector?
    >>> void* will do it.

    >>
    >> A little OO will do it even better!

    >
    > You misrepresented what I said by snipping the following, which is what
    > I replied to:
    >
    >> -> Is it possible to make a pointer, which can point to all of these
    >> tuples?)

    >
    > Plonk.


    Huh? Is this a reason to plonk?

    The OP didn't fully specify if he want to _store_ or _point_ to different
    types, he wrote:

    > // -> Is it possible to store those e.g. to a vector?


    and:

    > -> Is it possible to make a pointer, which can point to all of these
    > tuples?)


    So one way is to point to these tuples with a void*, but there is no way to
    find the type of the pointed object afterwards.

    A better way is to use some more OO concepts, where you can hold the type
    together with the pointer, or store the tuples directly into the vector,
    depends on the needs of the OP.

    So "A little OO will do it even better!" is a valid suggestion in my eyes.

    To the OP:
    I would recommend boost::variant or boost::any.

    With both, you can store the pointers to the tuples, or the tuples directly
    in the container.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
     
    Thomas J. Gritzan, Oct 6, 2006
    #6
    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. Saraswati lakki
    Replies:
    0
    Views:
    1,428
    Saraswati lakki
    Jan 6, 2012
  2. Claira
    Replies:
    0
    Views:
    134
    Claira
    Feb 17, 2013
  3. Tim Golden
    Replies:
    2
    Views:
    196
    Rick Johnson
    Feb 17, 2013
  4. Oscar Benjamin
    Replies:
    8
    Views:
    213
    Dennis Lee Bieber
    Feb 19, 2013
  5. Jon Reyes
    Replies:
    18
    Views:
    276
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page