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

F

fff_afafaf

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

mlimber

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
 
P

Pete Becker

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.
 
P

Pete Becker

Roland said:
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.
 
T

Thomas J. Gritzan

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


Plonk.

Huh? Is this a reason to plonk?

The OP didn't fully specify if he want to _store_ or _point_ to different
// -> 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top