Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Making a std::string a member of a union ???

Reply
Thread Tools

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

 
 
Dave Steffen
Guest
Posts: n/a
 
      01-09-2007
"Peter Olcott" <> writes:

> "Rolf Magnus" <> wrote in message
> news:eo0mev$3nd$01$...

[...]

> > 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)
 
Reply With Quote
 
 
 
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Dave Steffen" <> wrote in message
news:...
> "Peter Olcott" <> writes:
>
>> "Rolf Magnus" <> wrote in message
>> news:eo0mev$3nd$01$...

> [...]
>
>> > 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)



 
Reply With Quote
 
 
 
 
Simon G Best
Guest
Posts: n/a
 
      01-09-2007
Peter Olcott wrote:
> "Ron Natalie" <> wrote in message
> news:45a3e11f$0$28072$ m...
>> 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?
 
Reply With Quote
 
Simon G Best
Guest
Posts: n/a
 
      01-09-2007
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?
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Simon G Best" <> wrote in message
news:2-...
> Peter Olcott wrote:
>> "Ron Natalie" <> wrote in message
>> news:45a3e11f$0$28072$ m...
>>> 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?



 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Simon G Best" <> wrote in message
news:2-...
> 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?



 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-09-2007


> 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.
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-09-2007
Peter Olcott wrote:
> "Simon G Best" <> wrote in message
> news:2-...
>> 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)

 
Reply With Quote
 
Simon G Best
Guest
Posts: n/a
 
      01-09-2007
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?
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Ron Natalie" <> wrote in message
news:45a3fcd7$0$28100$ m...
>
>
>> 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(){};


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple union member initialization Ricky Lung C++ 5 08-19-2004 09:07 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
Setting union member in structure Jeff Massung C++ 2 12-22-2003 05:53 PM



Advertisments