473,507 Members | 13,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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
5 2347
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3015
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs...
10
1666
by: Xavier Noria | last post by:
Given two structures of the same size but different type, does C99 guarantee that pointers to them can be casted one to each other, and that the order of the elements will be kind of respected? ...
9
2416
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
1
3260
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
20
2593
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
17
5028
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
4
4015
by: Daniel Marques | last post by:
I would like to know how to get the address of a container of the STL without using iterators. I have a class with three STL lists and a index which points to an element of any ot the other lists,...
15
3503
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
9
1911
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.