472,102 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

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 #1
84 15270
"Peter Olcott" <No****@SeeScreen.comwrote 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.".
Jan 9 '07 #2
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
Jan 9 '07 #3

"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99. 21...
"Peter Olcott" <No****@SeeScreen.comwrote 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.
Jan 9 '07 #4
"Peter Olcott" <No****@SeeScreen.comwrote in
news:1q*******************@newsfe24.lga:
>
"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99. 21...
>"Peter Olcott" <No****@SeeScreen.comwrote 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?
Jan 9 '07 #5

"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99. 21...
"Peter Olcott" <No****@SeeScreen.comwrote in
news:1q*******************@newsfe24.lga:
>>
"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99 .21...
>>"Peter Olcott" <No****@SeeScreen.comwrote 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.
Jan 9 '07 #6
Peter Olcott wrote:
>
"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99. 21...
>"Peter Olcott" <No****@SeeScreen.comwrote 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."

Jan 9 '07 #7
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
Jan 9 '07 #8

"Rolf Magnus" <ra******@t-online.dewrote in message
news:eo*************@news.t-online.com...
Peter Olcott wrote:
>>
"Andre Kostur" <nn******@kostur.netwrote in message
news:Xn*******************************@209.135.99 .21...
>>"Peter Olcott" <No****@SeeScreen.comwrote 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?
Jan 9 '07 #9

"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@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

Jan 9 '07 #10
"Peter Olcott" <No****@SeeScreen.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*******@numerica.uswrote in message
news:qq*************@yttrium.numerica.us...
"Peter Olcott" <No****@SeeScreen.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.netwrote in message
news:45***********************@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.
"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**********@btinternet.comwrote in message
news:2-*********************@bt.com...
Peter Olcott wrote:
>"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.c om...
>>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**********@btinternet.comwrote 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**********@btinternet.comwrote 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.netwrote in message
news:45***********************@news.newshosting.co 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(){};
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
Peter Olcott wrote:
"Simon G Best" <si**********@btinternet.comwrote in message
news:2-*********************@bt.com...
>>
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.
Sounds like you probably want runtime polymorphism, if I'm guessing
right. That means deriving from base classes, having virtual functions,
and that sort of thing. Much better than unions :-)

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

"Simon G Best" <si**********@btinternet.comwrote in message
news:o_******************************@bt.com...
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() {}
};
So it looks like you are saying that Simple() invokes something() therefore
simple does something and I am wrong that Simple(){} is empty of semantics.
>
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 #22

"Gianni Mariani" <gi*******@mariani.wswrote in message
news:45***********************@per-qv1-newsreader-01.iinet.net.au...
Peter Olcott wrote:
>"Simon G Best" <si**********@btinternet.comwrote 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)
If all that I want is std::string could I simply use [ std::string* String ] in
my union?
Jan 9 '07 #23
Peter Olcott wrote:
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.co 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(){};

Trivial has a specific definition. I already posted what it is. It is
completely in contradiction with your snippet above.
Jan 9 '07 #24
Peter Olcott wrote:
>
If all that I want is std::string could I simply use [ std::string* String ] in
my union?

You would need management code to allocate and deallocate it.
Jan 9 '07 #25

"Simon G Best" <si**********@btinternet.comwrote in message
news:Iv*********************@bt.com...
Peter Olcott wrote:
>"Simon G Best" <si**********@btinternet.comwrote in message
news:2-*********************@bt.com...
>>>
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.

Sounds like you probably want runtime polymorphism, if I'm guessing right.
That means deriving from base classes, having virtual functions, and that sort
of thing. Much better than unions :-)
I don't think that it is a good fit for runtime polymorphism because I will have
datatypes with disjoint sets of operations, such as std::string and double.
runtime polymorphism is the best fit when the sets of operations are identical,
yet their specific implementation varies. This is not one of those cases.
>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 9 '07 #26

"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.co m...
Peter Olcott wrote:
>"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.c om...
>>>
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(){};
Trivial has a specific definition. I already posted what it is. It is
completely in contradiction with your snippet above.
Yeah, Simon proved that I was wrong.
Jan 9 '07 #27

"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.co m...
Peter Olcott wrote:
>>
If all that I want is std::string could I simply use [ std::string* String ]
in my union?

You would need management code to allocate and deallocate it.
I already figured that, are there any other issues?
Jan 9 '07 #28
Peter Olcott wrote:
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.co m...
>Peter Olcott wrote:
>>If all that I want is std::string could I simply use [ std::string* String ]
in my union?
You would need management code to allocate and deallocate it.

I already figured that, are there any other issues?

You need to remember which element you stored in the vector.

Once you do all that you probably have reimplemented boost::any.
Jan 9 '07 #29
Peter Olcott wrote:
>
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(){};
The standard actually defines what it means by "non-trivial
constructor". And another respondent has already summarised it.

Anyway:-

[Start C++ snippet.]

#include <iostream>
#include <string>

class base {
public:
base() { std::clog << "Looky here!" << std::endl;
};

class foo : public base {
std::string s;
public:
foo() {} // This is non-trivial.
};

class bar {
std::string s;
public:
bar() {} // This is non-trivial, too.
};

[End C++ snippet.]

Neither foo::foo() nor bar::bar() are trivial. Both construct s
(implicitly), but only foo::foo() constructs base as well. We /could/
then say that bar::bar() is closer to being trivial than foo::foo().

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 9 '07 #30
Peter Olcott wrote:
>
So it looks like you are saying that Simple() invokes something() therefore
simple does something and I am wrong that Simple(){} is empty of semantics.
Yep :-)

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

Peter Olcott wrote in message <1q*******************@newsfe24.lga>...
>
"Andre Kostur" <nn******@kostur.netwrote in message ...
>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.
Storage? What good is a String class that can't store anything?

Do you understand what a union is?

std::cout<<" sizeof(std::string) ="
<<sizeof(std::string)<<std::endl;
std::cout<<" sizeof(double) ="
<<sizeof(double)<<std::endl;
// out: sizeof(std::string) =4
// out: sizeof(double) =8

So, if you did (if it compiled):

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

.... the union would be the size of a double (since it is bigger than
std::string (on my machine)).

Try it this way:

#include <iostream // #include <ostream>
#include <string>

union AnyType {
std::string *String; // pointer
double Number;
};

int main(){
AnyType AnyT;
std::string Sting("AnyType AnyT;");
AnyT.String = &Sting;
std::cout<<" AnyT.String ="<<*(AnyT.String)<<std::endl;
AnyT.Number = 543.123;
std::cout<<" AnyT.Number ="<<AnyT.Number<<std::endl;
// oops! // std::cout<<" AnyT.String ="<<*(AnyT.String)<<std::endl;
return 0;
} // main()

But, the union still doesn't seem logical to me.
--
Bob R
POVrookie
Jan 9 '07 #32

"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.co m...
Peter Olcott wrote:
>"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.c om...
>>Peter Olcott wrote:

If all that I want is std::string could I simply use [ std::string*
String ] in my union?
You would need management code to allocate and deallocate it.

I already figured that, are there any other issues?
You need to remember which element you stored in the vector.

Once you do all that you probably have reimplemented boost::any.
Okay then this is the way that I will do it.
Jan 9 '07 #33
Peter Olcott a écrit :
Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
Use pointer instead.
union AnyType
{
std::string* str;
double number;
};
And don't forget to delete it before writing into the
structure(embedding that in a structure with accessor).

Or use boost::variant or boost::any as Thomas J. Gritzan wrote.

Michael
Jan 10 '07 #34

"Michael DOUBEZ" <mi************@free.frwrote in message
news:45**********************@news.free.fr...
Peter Olcott a écrit :
>Is there anyway of doing this besides making my own string from scratch?

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

Use pointer instead.
union AnyType
{
std::string* str;
double number;
};
And don't forget to delete it before writing into the structure(embedding that
in a structure with accessor).
I don't know what you mean by this.

>
Or use boost::variant or boost::any as Thomas J. Gritzan wrote.

Michael

Jan 10 '07 #35
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:P5******************@newsfe18.lga...
>
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.co m...
>Peter Olcott wrote:
>>"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting. com...
Peter Olcott wrote:

If all that I want is std::string could I simply use [ std::string*
String ] in my union?
You would need management code to allocate and deallocate it.

I already figured that, are there any other issues?
You need to remember which element you stored in the vector.

Once you do all that you probably have reimplemented boost::any.

Okay then this is the way that I will do it.
It depends on what you are actually tryign to do. A union of a double and a
pointer doesn't seem like it will do you any good. It sounds more like you
want to refer to this double as either a double, or as a std::string,
correct? It would seem a simple class would work for you. Something like:

#include <iostream>
#include <string>
#include <sstream>

class AnyType
{
public:
operator double() const { return Val_; }
operator std::string () const
{
std::stringstream Convert;
Convert << Val_;
return Convert.str();
}
AnyType* operator= ( const double Val ) { Val_ = Val; return this; }
AnyType* operator= ( const std::string& Val )
{
std::stringstream Convert;
Convert << Val;
Convert >Val_;
return this;
}
private:
double Val_;
};

int main()
{
AnyType MyDouble;
MyDouble = 1234.56;
std::cout << MyDouble << "\n";
MyDouble = "2345.67";
std::cout << MyDouble << "\n";

std::string wait;
std::getline( std::cin, wait );
}

This could easily be converted to a template for any type.

Is this the kind of thing you are looking for?
Jan 10 '07 #36
Peter Olcott a écrit :
"Michael DOUBEZ" <mi************@free.frwrote in message
news:45**********************@news.free.fr...
>Peter Olcott a écrit :
>>Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
Use pointer instead.
union AnyType
{
std::string* str;
double number;
};

>And don't forget to delete it before writing into the structure(embedding that
in a structure with accessor).

I don't know what you mean by this.
I dont pretend it is good C++ design but it looks like what you ask for.

Accessor solution is something like this (not tested):
struct GenericContent
{
public:
//! supported types
enum mytype{ type_none, type_str, type_number };

//constructor - destructor
GenericContent():_type(type_none){}
~GenericContent()
{ //clean internal data
this->clean_me();
}
//! setters
void setString(const std::string& str)
{ //clean internal data
this->clean_me();
this->_type=type_str;
this->_data.str=new std::string(str);
}
void setNumber(double num)
{ //clean internal data
this->clean_me();
this->_type=type_number;
this->_data.number=num;
}

//! accessors
mytype getType()const
{
return this->_type;
}
const string& getString()const
{
assert(this->_type==type_str);
return *(this->_data.str);
}
double getNumber()const
{
assert(this->_type==type_number);
return this->_data.number;
}
private:
//! clean internal data and reset to none
void clean_me()
{
if(this->_type==type_str)
{
delete this->_data.str;
}
this->_type=type_none;
}

//! refuse copy ...
GenericContent(const GenericContent&);
GenericContent& operator=(const GenericContent& genc);

private:
//! store real content of data
mytype _type;
//! data
union
{
std::string* str;
double number;
} _data;
};

Jan 10 '07 #37
I can't tell what you are trying to do here, but, the std::string will probably
not hold data that can be converted to and from string.

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:Hk*************@newsfe06.lga...
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:P5******************@newsfe18.lga...
>>
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting.c om...
>>Peter Olcott wrote:
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting .com...
Peter Olcott wrote:
>
>If all that I want is std::string could I simply use [ std::string*
>String ] in my union?
You would need management code to allocate and deallocate it.

I already figured that, are there any other issues?
You need to remember which element you stored in the vector.

Once you do all that you probably have reimplemented boost::any.

Okay then this is the way that I will do it.

It depends on what you are actually tryign to do. A union of a double and a
pointer doesn't seem like it will do you any good. It sounds more like you
want to refer to this double as either a double, or as a std::string, correct?
It would seem a simple class would work for you. Something like:

#include <iostream>
#include <string>
#include <sstream>

class AnyType
{
public:
operator double() const { return Val_; }
operator std::string () const
{
std::stringstream Convert;
Convert << Val_;
return Convert.str();
}
AnyType* operator= ( const double Val ) { Val_ = Val; return this; }
AnyType* operator= ( const std::string& Val )
{
std::stringstream Convert;
Convert << Val;
Convert >Val_;
return this;
}
private:
double Val_;
};

int main()
{
AnyType MyDouble;
MyDouble = 1234.56;
std::cout << MyDouble << "\n";
MyDouble = "2345.67";
std::cout << MyDouble << "\n";

std::string wait;
std::getline( std::cin, wait );
}

This could easily be converted to a template for any type.

Is this the kind of thing you are looking for?

Jan 10 '07 #38
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:Zt*****************@newsfe21.lga...
Is there anyway of doing this besides making my own string from scratch?

union AnyType {
std::string String;
double Number;
};
If you really want to union the memory footprint of types with non-trivial
constructors, you could wrap them in a container which has a trivial
constructor. Unfortunately, this will currently not yield very portable code
as you won't have any control over alignment, but it will probably work on
most platforms:

#include <string>

template<class Tclass UnionMember
{
public:
void construct()
{ new (*this) T(); }
template<class P1void construct (const P1 & p1)
{ new (*this) T(p1); }
template<class P1, class P2void construct (const P1 & p1, const P2 &
p2)
{ new (*this) T(p1, p2); }
// etc.

void destroy() { (*this)->~T(); }

operator T * () { return reinterpret_cast<T*>(m_data); }
operator const T * () const { return reinterpret_cast<const
T*>(m_data); }

T * operator -() { return operator T*(); }
const T * operator -() const { return operator const T*(); }

private:
char m_data[sizeof(T)]; // for true portable code you'll need to align
this buffer to meet the alignment requirements for T.
};

union MyUnion
{
UnionMember<std::stringmyString;
double myDouble;
};

int main()
{
MyUnion u;

// construct a string within the union
u.myString.construct("hi there");

// assign it with a different string
*u.myString = "goodbye";

// assign the double - don't forget to destroy the string first!
u.myString.destroy(); // destroy the string
u.myDouble = 34.5; // and assign the double.
}

You should take care to construct every object on it's first use and
destruct it before using another value in the union, or when you dispose of
the union.

The next C++ standard revision will most probably include methods of
retrieving and controlling alignment of datamembers.

- Sylvester
Jan 10 '07 #39
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:Hk*************@newsfe06.lga...
>"Peter Olcott" <No****@SeeScreen.comwrote in message
news:P5******************@newsfe18.lga...
>>>
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting. com...
Peter Olcott wrote:
"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshostin g.com...
>Peter Olcott wrote:
>>
>>If all that I want is std::string could I simply use [ std::string*
>>String ] in my union?
>You would need management code to allocate and deallocate it.
>
I already figured that, are there any other issues?
You need to remember which element you stored in the vector.

Once you do all that you probably have reimplemented boost::any.

Okay then this is the way that I will do it.

It depends on what you are actually tryign to do. A union of a double
and a pointer doesn't seem like it will do you any good. It sounds more
like you want to refer to this double as either a double, or as a
std::string, correct? It would seem a simple class would work for you.
Something like:

#include <iostream>
#include <string>
#include <sstream>

class AnyType
{
public:
operator double() const { return Val_; }
operator std::string () const
{
std::stringstream Convert;
Convert << Val_;
return Convert.str();
}
AnyType* operator= ( const double Val ) { Val_ = Val; return this; }
AnyType* operator= ( const std::string& Val )
{
std::stringstream Convert;
Convert << Val;
Convert >Val_;
return this;
}
private:
double Val_;
};

int main()
{
AnyType MyDouble;
MyDouble = 1234.56;
std::cout << MyDouble << "\n";
MyDouble = "2345.67";
std::cout << MyDouble << "\n";

std::string wait;
std::getline( std::cin, wait );
}

This could easily be converted to a template for any type.

Is this the kind of thing you are looking for?
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:mZ****************@newsfe15.lga...
>I can't tell what you are trying to do here, but, the std::string will
probably not hold data that can be converted to and from string.
Please don't top post. Message rearranged.

You mean not hold data that can be converted to and from a double?

Okay, so you want your union to hold *either* a std::string or a double.
There are a few ways to do this including templates, templates with common
base class, pointers, etc...

How is it you want to be able to use AnyType?


Jan 10 '07 #40

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:9Q***********@newsfe03.lga...
>"Jim Langston" <ta*******@rocketmail.comwrote in message
news:Hk*************@newsfe06.lga...
>>"Peter Olcott" <No****@SeeScreen.comwrote in message
news:P5******************@newsfe18.lga...

"Ron Natalie" <ro*@spamcop.netwrote in message
news:45***********************@news.newshosting .com...
Peter Olcott wrote:
>"Ron Natalie" <ro*@spamcop.netwrote in message
>news:45***********************@news.newshosti ng.com...
>>Peter Olcott wrote:
>>>
>>>If all that I want is std::string could I simply use [ std::string*
>>>String ] in my union?
>>You would need management code to allocate and deallocate it.
>>
>I already figured that, are there any other issues?
You need to remember which element you stored in the vector.
>
Once you do all that you probably have reimplemented boost::any.

Okay then this is the way that I will do it.

It depends on what you are actually tryign to do. A union of a double and a
pointer doesn't seem like it will do you any good. It sounds more like you
want to refer to this double as either a double, or as a std::string,
correct? It would seem a simple class would work for you. Something like:

#include <iostream>
#include <string>
#include <sstream>

class AnyType
{
public:
operator double() const { return Val_; }
operator std::string () const
{
std::stringstream Convert;
Convert << Val_;
return Convert.str();
}
AnyType* operator= ( const double Val ) { Val_ = Val; return this; }
AnyType* operator= ( const std::string& Val )
{
std::stringstream Convert;
Convert << Val;
Convert >Val_;
return this;
}
private:
double Val_;
};

int main()
{
AnyType MyDouble;
MyDouble = 1234.56;
std::cout << MyDouble << "\n";
MyDouble = "2345.67";
std::cout << MyDouble << "\n";

std::string wait;
std::getline( std::cin, wait );
}

This could easily be converted to a template for any type.

Is this the kind of thing you are looking for?

"Peter Olcott" <No****@SeeScreen.comwrote in message
news:mZ****************@newsfe15.lga...
>>I can't tell what you are trying to do here, but, the std::string will
probably not hold data that can be converted to and from string.

Please don't top post. Message rearranged.

You mean not hold data that can be converted to and from a double?

Okay, so you want your union to hold *either* a std::string or a double. There
are a few ways to do this including templates, templates with common base
class, pointers, etc...

How is it you want to be able to use AnyType?
Its like I am making my own VARIANT record. I need some features that VARIANT
does not have.
Jan 10 '07 #41
Peter Olcott wrote:
"Simon G Best" <si**********@btinternet.comwrote in message
news:Iv*********************@bt.com...
>Peter Olcott wrote:
>>"Simon G Best" <si**********@btinternet.comwrote in message
news:2-*********************@bt.com...
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.
Sounds like you probably want runtime polymorphism, if I'm guessing right.
That means deriving from base classes, having virtual functions, and that sort
of thing. Much better than unions :-)

I don't think that it is a good fit for runtime polymorphism because I will have
datatypes with disjoint sets of operations, such as std::string and double.
runtime polymorphism is the best fit when the sets of operations are identical,
yet their specific implementation varies. This is not one of those cases.
The sets of operations don't have to be identical for runtime
polymorphism to be appropriate. And, as you seem to be finding, unions
don't seem to be suitable for your needs, either.

Why do you need or want to use a union? What problem is the union
supposed to solve? Is it that you want to store your "various elemental
data types" in the same container as if they're all of the same type
when they're not? Is it because they're all "elemental data types"
while, at the same time, they're various, different "elemental data
types"? If so, then runtime polymorphism is most probably exactly what
you need, as that's what polymorphism is very much about - it's pretty
much what the word 'polymorphism' means!

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Jan 10 '07 #42
Peter Olcott wrote:
>
Its like I am making my own VARIANT record. I need some features that VARIANT
does not have.
Is that "VARIANT record" in the Pascal sense? If it is, then I really
do think you probably want runtime polymorphism, as that is the C++ way
of doing that kind of thing.

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

"Simon G Best" <si**********@btinternet.comwrote in message
news:cO******************************@bt.com...
Peter Olcott wrote:
>"Simon G Best" <si**********@btinternet.comwrote in message
news:Iv*********************@bt.com...
>>Peter Olcott wrote:
"Simon G Best" <si**********@btinternet.comwrote in message
news:2-*********************@bt.com...
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.
Sounds like you probably want runtime polymorphism, if I'm guessing right.
That means deriving from base classes, having virtual functions, and that
sort of thing. Much better than unions :-)

I don't think that it is a good fit for runtime polymorphism because I will
have datatypes with disjoint sets of operations, such as std::string and
double. runtime polymorphism is the best fit when the sets of operations are
identical, yet their specific implementation varies. This is not one of those
cases.

The sets of operations don't have to be identical for runtime polymorphism to
be appropriate. And, as you seem to be finding, unions don't seem to be
suitable for your needs, either.

Why do you need or want to use a union? What problem is the union supposed to
solve? Is it that you want to store your "various elemental data types" in
the same container as if they're all of the same type when they're not? Is it
because they're all "elemental data types" while, at the same time, they're
various, different "elemental data types"? If so, then runtime polymorphism
is most probably exactly what you need, as that's what polymorphism is very
much about - it's pretty much what the word 'polymorphism' means!
I am creating my own custom computer language interpreter. The language itself
is based on "C" and will interface with C++ native code.
>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 10 '07 #44

"Simon G Best" <si**********@btinternet.comwrote in message
news:We******************************@bt.com...
Peter Olcott wrote:

Its like I am making my own VARIANT record. I need some features that VARIANT
does not have.

Is that "VARIANT record" in the Pascal sense? If it is, then I really do
think you probably want runtime polymorphism, as that is the C++ way of doing
that kind of thing.
One reason that I can't use run-time polymorphism is that the interpreted
language that I am constructing can not directly interface with polymorphic
class members. It will only have the capabilities of "C" and not C++.
>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 10 '07 #45
Peter Olcott wrote:
"Simon G Best" <si**********@btinternet.comwrote in message
news:cO******************************@bt.com...
>>
Why do you need or want to use a union? What problem is the union supposed to
solve?
....
>
I am creating my own custom computer language interpreter. The language itself
is based on "C" and will interface with C++ native code.
That's far too vague and general. What's the /specific/ problem that
the union is supposed to solve? What is it /specifically/ that you're
trying to do?

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

"Simon G Best" <si**********@btinternet.comwrote in message
news:_s*********************@bt.com...
Peter Olcott wrote:
>"Simon G Best" <si**********@btinternet.comwrote in message
news:cO******************************@bt.com...
>>>
Why do you need or want to use a union? What problem is the union supposed
to solve?
...
>>
I am creating my own custom computer language interpreter. The language
itself is based on "C" and will interface with C++ native code.

That's far too vague and general. What's the /specific/ problem that the
union is supposed to solve? What is it /specifically/ that you're trying to
do?
I have a single set of memory locations that must be able to store data of a
fixed set of types. A language lacking the capability of C++ must interface with
this data. The data types must include all of the elemental types {char, int,
double} and one string type based on wide characters and something like a string
build from a std::vector of struct.
>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 10 '07 #47
Peter Olcott wrote:
>
I have a single set of memory locations that must be able to store data of a
fixed set of types. A language lacking the capability of C++ must interface with
this data. The data types must include all of the elemental types {char, int,
double} and one string type based on wide characters and something like a string
build from a std::vector of struct.
That's still too vague.

* What do you mean by "a single set of memory locations"? Is that from
the perspective of your interpreted language?

* When you say, "A language lacking the capability of C++ must
interface with this data", what do you mean by "interface"?

If you're trying to do what I /think/ you're trying to do, then I think
you're probably failing to properly separate your interpreted language
from your implementation of its interpreter. But as you're not at all
clear on what you're actually trying to do, I can only guess.

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

"Simon G Best" <si**********@btinternet.comwrote in message
news:nN******************************@bt.com...
Peter Olcott wrote:
>>
I have a single set of memory locations that must be able to store data of a
fixed set of types. A language lacking the capability of C++ must interface
with this data. The data types must include all of the elemental types {char,
int, double} and one string type based on wide characters and something like
a string build from a std::vector of struct.

That's still too vague.

* What do you mean by "a single set of memory locations"? Is that from the
perspective of your interpreted language?
It will be implemented as a std::vector<AnyTypeAny;
>
* When you say, "A language lacking the capability of C++ must interface with
this data", what do you mean by "interface"?
Read and write based on subscript.
>
If you're trying to do what I /think/ you're trying to do, then I think you're
probably failing to properly separate your interpreted language from your
implementation of its interpreter. But as you're not at all
I have no choice in this, the interpreted language is provided by a third party.
I am hooking this third party interpreted language into my system and then
exposing another different interpreted language implemented in terms of the
third party language.
clear on what you're actually trying to do, I can only guess.

--
Simon G Best
What happens if I mention Leader Kibo in my .signature?

Jan 11 '07 #49
"Peter Olcott" <No****@SeeScreen.comwrote in message
news:5K******************@newsfe19.lga...
>
"Simon G Best" <si**********@btinternet.comwrote in message
news:We******************************@bt.com...
>Peter Olcott wrote:
>
Its like I am making my own VARIANT record. I need some features that
VARIANT does not have.

Is that "VARIANT record" in the Pascal sense? If it is, then I really do
think you probably want runtime polymorphism, as that is the C++ way of
doing that kind of thing.

One reason that I can't use run-time polymorphism is that the interpreted
language that I am constructing can not directly interface with
polymorphic class members. It will only have the capabilities of "C" and
not C++.
>>
--
Simon G Best
What happens if I mention Leader Kibo in my .signature?
Okay, I think the easiest way would be to have a class that can store any of
the possible types of data. You'll need to overload operator= and operator
type for each type, then you'll want to store in this class what type is
actually being used. You'll have problems, however, when the type is
arbitary.

class AnyType
{
public:
operator std::string() { return StringVal; }
operator int() { return IntVal; }
// etc...
};

int main();
{
AnyType Foo;
// yada yada

std::cout << AnyType << "\n";
// ooops, what type is it supposed to output? std::string? int? float?
double? char? etc..
}

Assignments and constructors would be easier, because there will be a parm
that says what type it is.

AnyType Foo;
Foo = 12;
12 is an integer, and so would use operator=( const int ); no ambiguity
there.

AnyType Foo( 12.5 );
12.5 is a double, and so would use the constructor accepting double, so no
ambiguity there.

You will have problems when the type isn't known because of no parameter.
What is accepted in it's use?

std::cout << AnyType.val(INT) << "\n";
is something like that acceptable?
Jan 11 '07 #50

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Naren | last post: by
7 posts views Thread by Steven T. Hatton | last post: by
18 posts views Thread by ranjeet.gupta | last post: by
9 posts views Thread by Richard Lewis Haggard | last post: by
5 posts views Thread by mailforpr | last post: by
6 posts views Thread by steve.kim | last post: by
7 posts views Thread by Valeriu Catina | last post: by
reply views Thread by leo001 | last post: by

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.