Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Making a std::string a member of a union ??? (http://www.velocityreviews.com/forums/t459742-making-a-std-string-a-member-of-a-union.html)

Peter Olcott 01-09-2007 04:34 PM

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;
};



Andre Kostur 01-09-2007 04:52 PM

Re: Making a std::string a member of a union ???
 
"Peter Olcott" <NoSpam@SeeScreen.com> 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.".

Thomas J. Gritzan 01-09-2007 05:08 PM

Re: Making a std::string a member of a union ???
 
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

Peter Olcott 01-09-2007 05:38 PM

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

"Andre Kostur" <nntpspam@kostur.net> wrote in message
news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
> "Peter Olcott" <NoSpam@SeeScreen.com> 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.



Andre Kostur 01-09-2007 05:56 PM

Re: Making a std::string a member of a union ???
 
"Peter Olcott" <NoSpam@SeeScreen.com> wrote in
news:1qQoh.48816$rb2.35647@newsfe24.lga:

>
> "Andre Kostur" <nntpspam@kostur.net> wrote in message
> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>> "Peter Olcott" <NoSpam@SeeScreen.com> 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?

Peter Olcott 01-09-2007 06:06 PM

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

"Andre Kostur" <nntpspam@kostur.net> wrote in message
news:Xns98B365E55BA2Cnntpspamkosutrnet@209.135.99. 21...
> "Peter Olcott" <NoSpam@SeeScreen.com> wrote in
> news:1qQoh.48816$rb2.35647@newsfe24.lga:
>
>>
>> "Andre Kostur" <nntpspam@kostur.net> wrote in message
>> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>>> "Peter Olcott" <NoSpam@SeeScreen.com> 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.



Rolf Magnus 01-09-2007 06:20 PM

Re: Making a std::string a member of a union ???
 
Peter Olcott wrote:

>
> "Andre Kostur" <nntpspam@kostur.net> wrote in message
> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>> "Peter Olcott" <NoSpam@SeeScreen.com> 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."




Ron Natalie 01-09-2007 06:44 PM

Re: Making a std::string a member of a union ???
 
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

Peter Olcott 01-09-2007 06:54 PM

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

"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:eo0mev$3nd$01$1@news.t-online.com...
> Peter Olcott wrote:
>
>>
>> "Andre Kostur" <nntpspam@kostur.net> wrote in message
>> news:Xns98B35B1F7CB97nntpspamkosutrnet@209.135.99. 21...
>>> "Peter Olcott" <NoSpam@SeeScreen.com> 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?



Peter Olcott 01-09-2007 06:56 PM

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

"Ron Natalie" <ron@spamcop.net> wrote in message
news:45a3e11f$0$28072$9a6e19ea@news.newshosting.co 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





All times are GMT. The time now is 04:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.