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 ???

 
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007
Is there anyway of doing this besides making my own string from scratch?

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


 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-09-2007
"Peter Olcott" <> wrote in news:ZtPoh.8718$rv1.8337
@newsfe21.lga:

> Is there anyway of doing this besides making my own string from scratch?
>
> union AnyType {
> std::string String;
> double Number;
> };



You can't do this even with your own string:

Section 12.1.11: "A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.".
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      01-09-2007
Peter Olcott schrieb:
> Is there anyway of doing this besides making my own string from scratch?
>
> union AnyType {
> std::string String;
> double Number;
> };


You cannot. But depending on what you want to do, you might find
boost::variant or boost::any quite useful:

http://www.boost.org/doc/html/variant.html
http://www.boost.org/doc/html/any.html

--
Thomas
http://www.netmeister.org/news/learn2quote.html
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Andre Kostur" <> wrote in message
news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
> "Peter Olcott" <> wrote in news:ZtPoh.8718$rv1.8337
> @newsfe21.lga:
>
>> Is there anyway of doing this besides making my own string from scratch?
>>
>> union AnyType {
>> std::string String;
>> double Number;
>> };

>
>
> You can't do this even with your own string:
>
> Section 12.1.11: "A union member shall not be of a class type (or array
> thereof) that has a non-trivial constructor.".


If I create my own StringType class that has every other feature of std::string,
except nontrivial constructors, then it should work?

For example if every instance of StringType is always empty unless data is
explicitly added using operator=() or other means, then there would seem to be
no need for constructors.


 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      01-09-2007
"Peter Olcott" <> wrote in
news:1qQoh.48816$:

>
> "Andre Kostur" <> wrote in message
> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>> "Peter Olcott" <> wrote in
>> news:ZtPoh.8718$rv1.8337 @newsfe21.lga:
>>
>>> Is there anyway of doing this besides making my own string from
>>> scratch?
>>>
>>> union AnyType {
>>> std::string String;
>>> double Number;
>>> };

>>
>>
>> You can't do this even with your own string:
>>
>> Section 12.1.11: "A union member shall not be of a class type (or
>> array thereof) that has a non-trivial constructor.".

>
> If I create my own StringType class that has every other feature of
> std::string, except nontrivial constructors, then it should work?
>
> For example if every instance of StringType is always empty unless
> data is explicitly added using operator=() or other means, then there
> would seem to be no need for constructors.


Watch out if the members of your class have non-trivial constructors. What
I don't know offhand (hopefully someone else can elaborate), are simply
initializations enough to call it a non-trivial constructor? Ex:

class Simple
{
public:
Simple() : data(0), size(0) {};

private:
char * data;
size_t size;
};


Is that a non-trivial constructor?
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Andre Kostur" <> wrote in message
news:Xns98B365E55BA2Cnntpspamkosutrnet@209.135.99. 21...
> "Peter Olcott" <> wrote in
> news:1qQoh.48816$:
>
>>
>> "Andre Kostur" <> wrote in message
>> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>>> "Peter Olcott" <> wrote in
>>> news:ZtPoh.8718$rv1.8337 @newsfe21.lga:
>>>
>>>> Is there anyway of doing this besides making my own string from
>>>> scratch?
>>>>
>>>> union AnyType {
>>>> std::string String;
>>>> double Number;
>>>> };
>>>
>>>
>>> You can't do this even with your own string:
>>>
>>> Section 12.1.11: "A union member shall not be of a class type (or
>>> array thereof) that has a non-trivial constructor.".

>>
>> If I create my own StringType class that has every other feature of
>> std::string, except nontrivial constructors, then it should work?
>>
>> For example if every instance of StringType is always empty unless
>> data is explicitly added using operator=() or other means, then there
>> would seem to be no need for constructors.

>
> Watch out if the members of your class have non-trivial constructors. What
> I don't know offhand (hopefully someone else can elaborate), are simply
> initializations enough to call it a non-trivial constructor? Ex:
>
> class Simple
> {
> public:
> Simple() : data(0), size(0) {};
>
> private:
> char * data;
> size_t size;
> };
>
>
> Is that a non-trivial constructor?


I think that anything besides Simple(){}; is a non trivial constructor. This is
as trivial as trivial gets, syntax that is empty of semantics.


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

>
> "Andre Kostur" <> wrote in message
> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>> "Peter Olcott" <> wrote in news:ZtPoh.8718$rv1.8337
>> @newsfe21.lga:
>>
>>> Is there anyway of doing this besides making my own string from scratch?
>>>
>>> union AnyType {
>>> std::string String;
>>> double Number;
>>> };

>>
>>
>> You can't do this even with your own string:
>>
>> Section 12.1.11: "A union member shall not be of a class type (or array
>> thereof) that has a non-trivial constructor.".

>
> If I create my own StringType class that has every other feature of
> std::string, except nontrivial constructors, then it should work?
>
> For example if every instance of StringType is always empty unless data is
> explicitly added using operator=() or other means, then there would seem
> to be no need for constructors.


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



 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-09-2007
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.

A trivial constructor means that there is NONE of the
following:
1. No implicitly-declared default constructors
2. No virtual functions
3. No virtual base classes
4. All direct base classes have trivial constructors
5. All non-static members have trivial constructors
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      01-09-2007

"Rolf Magnus" <> wrote in message
news:eo0mev$3nd$01$...
> Peter Olcott wrote:
>
>>
>> "Andre Kostur" <> wrote in message
>> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>>> "Peter Olcott" <> wrote in news:ZtPoh.8718$rv1.8337
>>> @newsfe21.lga:
>>>
>>>> Is there anyway of doing this besides making my own string from scratch?
>>>>
>>>> union AnyType {
>>>> std::string String;
>>>> double Number;
>>>> };
>>>
>>>
>>> You can't do this even with your own string:
>>>
>>> Section 12.1.11: "A union member shall not be of a class type (or array
>>> thereof) that has a non-trivial constructor.".

>>
>> If I create my own StringType class that has every other feature of
>> std::string, except nontrivial constructors, then it should work?
>>
>> For example if every instance of StringType is always empty unless data is
>> explicitly added using operator=() or other means, then there would seem
>> to be no need for constructors.

>
> 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?


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

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

> A trivial constructor means that there is NONE of the
> following:
> 1. No implicitly-declared default constructors
> 2. No virtual functions
> 3. No virtual base classes
> 4. All direct base classes have trivial constructors
> 5. All non-static members have trivial constructors



 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57