By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

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

P: n/a

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,intt0(1, 2);
tuple<int,int, intt1(1, 2, 3);
tuple<int,longt2(1, 2);
tuple<int,std::stringt3(1, "2str");
tuple<int,std::string, intt4(1, "2string", 3);
tuple<int,intt5(1, 2);

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

return 0;
}

Oct 4 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ff********@yahoo.com 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.
Oct 4 '06 #2

P: n/a
ff********@yahoo.com 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,intt0(1, 2);
tuple<int,int, intt1(1, 2, 3);
tuple<int,longt2(1, 2);
tuple<int,std::stringt3(1, "2str");
tuple<int,std::string, intt4(1, "2string", 3);
tuple<int,intt5(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

Oct 4 '06 #3

P: n/a
On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <pe********@acm.org>
wrote:
>ff********@yahoo.com 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!
Oct 4 '06 #4

P: n/a
Roland Pibinger wrote:
On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <pe********@acm.org>
wrote:
>ff********@yahoo.com 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.
Oct 4 '06 #5

P: n/a
Pete Becker schrieb:
Roland Pibinger wrote:
>On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <pe********@acm.org>
wrote:
>>ff********@yahoo.com 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
Oct 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.