P
Peter Olcott
Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
union AnyType {
std::string String;
double Number;
};
Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
Peter said:Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
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.
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?
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.
I think that anything besides Simple(){}; is a non trivial constructor. This is
as trivial as trivial gets, syntax that is empty of semantics.
Well then how can I make a union of AnyType that includes something like aRolf 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."
I think that you must be wrong on this issue, you can't possibly get moreRon Natalie said:Peter Olcott wrote:
\
Nope, even that is a non-trivial constructor.
Peter Olcott said:news:[email protected]... [...]
Well then how can I make a union of AnyType that includes somethingSorry, 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."
like a std::string as one of its members?
Or a possibly much better way is to simply use a std::string* StringPtr;Dave Steffen said:Peter Olcott said:news:[email protected]... [...]
Well then how can I make a union of AnyType that includes somethingSorry, 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."
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.
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.
Peter said:Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
Simon G Best said:"Simple(){}" is not free of semantics. If it was, it would be literally
meaningless.
Simon G Best said:What do you want a union for? Generally, unions shouldn't be used.
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.
Peter said:I am creating my own computer language and I need a simple way to store the
various elemental data types.
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.
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.
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.