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,longt 2(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;
} 5 2358 ff********@yaho o.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. ff********@yaho o.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,longt 2(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
On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <pe********@acm .org>
wrote:
>ff********@yah oo.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!
Roland Pibinger wrote:
On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <pe********@acm .org>
wrote:
>ff********@yaho o.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.
Pete Becker schrieb:
Roland Pibinger wrote:
>On Wed, 04 Oct 2006 08:50:26 -0400, Pete Becker <pe********@acm .org> wrote:
>>ff********@yaho o.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 using ::pointer?
I did a quick experiment and replaced all the ::iterator in my code with
::pointer and it seems to work the same. What I'm think is on the surface
they're the same but perhaps there're some subtle differences beneath the...
|
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?
This code illustrates what I mean:
int main(void) {
struct foo { char c; };
struct bar { char c0; char c1; };
|
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, strings for logging,
and structures for data points.
I want to use one function to read data from this block and one function
to write data, for example:
sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate
object and
|
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
irrelavent. My syntax is surely where I am going wrong.
I'd like to be able to call this routine to read different values from
another device. This routine would be called quite simply as follows:
void main()
{
|
by: pinkfloydhomer |
last post by:
Is it well-defined and portable to do something like:
typedef struct
{
int type;
char c;
} S1;
typedef struct
{
| |
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 identifying memory
leaks. The application in question is the Mozilla Web Browser. I also
have had similar tasks before in the compiler construction area. And
it is easy to come up with many more examples, where such kind of
statistics can be very...
|
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, but i don't
know how to access these elements addresses.
Just as an example:
class C {
public:
int a;
}
|
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 you want to make a deque which can contain any
objects of any of those types. Normally what you would have to do is to
make a deque or vector of pointers of the base class type and then
allocate each object dynamically with 'new' and store the...
|
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 like this : (very simple..........)
010001010110001101010101010101010101010101
#include<stdio.h>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |