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

P

Peter Olcott

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

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

Andre Kostur

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

Peter Olcott

Andre Kostur said:
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.
 
A

Andre Kostur

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

Peter Olcott

Andre Kostur said:
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.
 
R

Rolf Magnus

Peter said:
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."
 
R

Ron Natalie

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
 
P

Peter Olcott

Rolf Magnus said:
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?
 
P

Peter Olcott

Ron Natalie said:
Peter Olcott wrote:
\

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

Dave Steffen

Peter Olcott said:
news:[email protected]... [...]
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.
 
P

Peter Olcott

Dave Steffen said:
Peter Olcott said:
news:[email protected]... [...]
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;
 
S

Simon G Best

Peter said:
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.
 
S

Simon G Best

Peter said:
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.
 
P

Peter Olcott

Simon G Best said:
"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.
 
P

Peter Olcott

Simon G Best said:
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.
 
R

Ron Natalie

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

Gianni Mariani

Peter said:
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)
 
S

Simon G Best

Peter said:
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.
 
P

Peter Olcott

Ron Natalie said:
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(){};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top