Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Different tuples to one container? (One type of a pointer to point to different kinds of tuples?)

Reply
Thread Tools

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

 
 
fff_afafaf@yahoo.com
Guest
Posts: n/a
 
      10-04-2006

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;
}

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-04-2006
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

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      10-04-2006
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.
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      10-04-2006
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!
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-04-2006
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.
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      10-05-2006
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
 
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
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Re: Can someone tell me what kinds of questions should be asked inthis list and what kinds in the tutor section? Oscar Benjamin Python 8 02-19-2013 12:12 AM
Re: Can someone tell me what kinds of questions should be asked inthis list and what kinds in the tutor section? Tim Golden Python 2 02-17-2013 09:31 PM
Can someone tell me what kinds of questions should be asked in thislist and what kinds in the tutor section? Claira Python 0 02-17-2013 12:19 AM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57