473,897 Members | 3,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making a std::string a member of a union ???

Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
Jan 9 '07
84 15960
"Peter Olcott" <No****@SeeScre en.comwrites:
"Rolf Magnus" <ra******@t-online.dewrote in message
news:eo******** *****@news.t-online.com...
[...]
Sorry, but no. 9.5 says: "An object of a class with a non-trivial
constructor, a non-trivial copy constructor, a non-trivial
destructor, or a non-trivial copy assignment operator cannot be a
member of a union, nor can an array of such objects."
Well then how can I make a union of AnyType that includes something
like a std::string as one of its members?
You don't.

One reasonably correct way to think of it is that only PODs (Plain
Old Data), e.g. C-style structs, can be part of a union.

The closest you could get would be some sort of primitive struct
with, say, a pointer-to-char and an integer to hold the string
length, with all the memory management and such dealt with
manually... and (this is the important part) no constructors or
destructors.

----------------------------------------------------------------------
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)
Jan 9 '07 #11

"Dave Steffen" <dg*******@nume rica.uswrote in message
news:qq******** *****@yttrium.n umerica.us...
"Peter Olcott" <No****@SeeScre en.comwrites:
>"Rolf Magnus" <ra******@t-online.dewrote in message
news:eo******* ******@news.t-online.com...
[...]
Sorry, but no. 9.5 says: "An object of a class with a non-trivial
constructor, a non-trivial copy constructor, a non-trivial
destructor, or a non-trivial copy assignment operator cannot be a
member of a union, nor can an array of such objects."

Well then how can I make a union of AnyType that includes something
like a std::string as one of its members?

You don't.

One reasonably correct way to think of it is that only PODs (Plain
Old Data), e.g. C-style structs, can be part of a union.

The closest you could get would be some sort of primitive struct
with, say, a pointer-to-char and an integer to hold the string
length, with all the memory management and such dealt with
manually... and (this is the important part) no constructors or
destructors.
Or a possibly much better way is to simply use a std::string* StringPtr;
----------------------------------------------------------------------
Dave Steffen, Ph.D. Disobey this command!
Software Engineer IV - Douglas Hofstadter
Numerica Corporation
dg@steffen a@t numerica d@ot us (remove @'s to email me)

Jan 9 '07 #12
Peter Olcott wrote:
"Ron Natalie" <ro*@spamcop.ne twrote in message
news:45******** *************** @news.newshosti ng.com...
>Peter Olcott wrote:
\
>>I think that anything besides Simple(){}; is a non trivial constructor. This
is as trivial as trivial gets, syntax that is empty of semantics.
Nope, even that is a non-trivial constructor.
I think that you must be wrong on this issue, you can't possibly get more
trivial than syntax that is completely empty of corresponding semantics.
"Simple(){} " is not free of semantics. If it was, it would be literally
meaningless.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 9 '07 #13
Peter Olcott wrote:
Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
What do you want a union for? Generally, unions shouldn't be used.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 9 '07 #14

"Simon G Best" <si**********@b tinternet.comwr ote in message
news:2-*************** ******@bt.com.. .
Peter Olcott wrote:
>"Ron Natalie" <ro*@spamcop.ne twrote in message
news:45******* *************** *@news.newshost ing.com...
>>Peter Olcott wrote:
\
I think that anything besides Simple(){}; is a non trivial constructor.
This is as trivial as trivial gets, syntax that is empty of semantics.
Nope, even that is a non-trivial constructor.
I think that you must be wrong on this issue, you can't possibly get more
trivial than syntax that is completely empty of corresponding semantics.

"Simple(){} " is not free of semantics. If it was, it would be literally
meaningless.
When I am saying that it is entirely free of semantics, I mean at the
programming level, not at the human communication level. In other words the
above statement has no corresponding machine code that is generated from the
compilation process. It translates into nothing at all.
>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 9 '07 #15

"Simon G Best" <si**********@b tinternet.comwr ote in message
news:2-*************** ******@bt.com.. .
Peter Olcott wrote:
>Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};

What do you want a union for? Generally, unions shouldn't be used.
I am creating my own computer language and I need a simple way to store the
various elemental data types.
>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 9 '07 #16

When I am saying that it is entirely free of semantics, I mean at the
programming level, not at the human communication level. In other words the
above statement has no corresponding machine code that is generated from the
compilation process. It translates into nothing at all.
UNTRUE. It does not translate to nothing at all. It specifically
changes the behavior of the class it is defined in. It specifically
changes the object into a non-trivial constructed one.
Jan 9 '07 #17
Peter Olcott wrote:
"Simon G Best" <si**********@b tinternet.comwr ote in message
news:2-*************** ******@bt.com.. .
>Peter Olcott wrote:
>>Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
What do you want a union for? Generally, unions shouldn't be used.

I am creating my own computer language and I need a simple way to store the
various elemental data types.
boost::any (or somthing similar) should do what you want. They do all
the copy/destruct work that unions don't and you need for std::string.

I wrote a similar beast at::Any. You can get it from:
http://netcabletv.org/public_releases/

(warning - it's big, it contains a number of precompiled libs)

Jan 9 '07 #18

"Ron Natalie" <ro*@spamcop.ne twrote in message
news:45******** *************** @news.newshosti ng.com...
>
>When I am saying that it is entirely free of semantics, I mean at the
programming level, not at the human communication level. In other words the
above statement has no corresponding machine code that is generated from the
compilation process. It translates into nothing at all.

UNTRUE. It does not translate to nothing at all. It specifically
changes the behavior of the class it is defined in. It specifically
changes the object into a non-trivial constructed one.
Okay since the official standard specifically refers to non trivial
constructors, try and provide an example of a non trivial constructor that is
more trivial than:
ClassName(){};
Jan 9 '07 #19
Peter Olcott wrote:
>
When I am saying that it is entirely free of semantics, I mean at the
programming level, not at the human communication level. In other words the
above statement has no corresponding machine code that is generated from the
compilation process. It translates into nothing at all.
This is just wrong.

Consider the following:-

[Start C++ snippet.]

#include <iostream>

class something {
public:
something() { std::clog << "Oh, look!" << std::endl; }
};

class Simple {
something a;
public:
Simple() {}
};

void foo() { Simple x; }

[End C++ snippet.]

What happens when you call foo()?

Also consider the case where Simple::Simple( ) is declared in a header,
for inclusion in multiple translation units, but defined elsewhere.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 9 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10883
by: prettysmurfed | last post by:
Hi all I have a bit of a problem, the subject of this post is almost selfexplaing. But here goes: Heres an example of the code I want to implement, its all nice and simple, but the flaw is I can't seem to get the adress of the member function stated properly in the DialogBox function. I thought it was enough to write it as Dialog::DlgProc, if it was a normal situation and DlgProc was a normal function (not member of anything) then you...
5
9392
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. what does a private static member function mean? Thaanx in advance for any advice Rgds,
7
2501
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take the names of other functions as arguments don't like the way I'm passing the member functions of my...
18
2387
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and compiler), and it is, what I conclude from the below code union data_type {
9
1394
by: Richard Lewis Haggard | last post by:
What is the logic for dropping the C++ prepending of 'm_' from member variables? Has there been a consensus developed on how to discriminate between class members and automatic temp variables? -- Richard Lewis Haggard
11
4441
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
5
1588
by: mailforpr | last post by:
Hello. Given is the following class: class Test { public: int i; int& get() { return i; } };
2
1250
by: Zytan | last post by:
I want to extend an existing class (a standard control), by making more member functions, to improve the ease of its use. *I don't want to make my own control, or add new functionality.* I just want to improve the *interface* for existing functionality. So, instead of doing this: MyUnit.MyFunction(Control, ...) I want: Control.MyFunction(...)
6
2590
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
7
2188
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
0
11250
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, 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...
1
10941
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,...
0
10478
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 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...
1
8032
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7185
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();...
0
6074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4698
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4293
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3300
bsmnconsultancy
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...

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.