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

Discussion in 'C++' started by Peter Olcott, Jan 9, 2007.

  1. Peter Olcott

    Peter Olcott Guest

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

    union AnyType {
    std::string String;
    double Number;
    };
    Peter Olcott, Jan 9, 2007
    #1
    1. Advertising

  2. Peter Olcott

    Andre Kostur Guest

    "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.".
    Andre Kostur, Jan 9, 2007
    #2
    1. Advertising

  3. Thomas J. Gritzan, Jan 9, 2007
    #3
  4. Peter Olcott

    Peter Olcott Guest

    "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.
    Peter Olcott, Jan 9, 2007
    #4
  5. Peter Olcott

    Andre Kostur Guest

    "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?
    Andre Kostur, Jan 9, 2007
    #5
  6. Peter Olcott

    Peter Olcott Guest

    "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.
    Peter Olcott, Jan 9, 2007
    #6
  7. Peter Olcott

    Rolf Magnus Guest

    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."
    Rolf Magnus, Jan 9, 2007
    #7
  8. Peter Olcott

    Ron Natalie Guest

    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
    Ron Natalie, Jan 9, 2007
    #8
  9. Peter Olcott

    Peter Olcott Guest

    "Rolf Magnus" <> wrote in message
    news:eo0mev$3nd$01$-online.com...
    > 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?
    Peter Olcott, Jan 9, 2007
    #9
  10. Peter Olcott

    Peter Olcott Guest

    "Ron Natalie" <> wrote in message
    news:45a3e11f$0$28072$...
    > 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
    Peter Olcott, Jan 9, 2007
    #10
  11. Peter Olcott

    Dave Steffen Guest

    "Peter Olcott" <> writes:

    > "Rolf Magnus" <> wrote in message
    > news:eo0mev$3nd$01$-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)
    Dave Steffen, Jan 9, 2007
    #11
  12. Peter Olcott

    Peter Olcott Guest

    "Dave Steffen" <> wrote in message
    news:...
    > "Peter Olcott" <> writes:
    >
    >> "Rolf Magnus" <> wrote in message
    >> news:eo0mev$3nd$01$-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)
    Peter Olcott, Jan 9, 2007
    #12
  13. Peter Olcott

    Simon G Best Guest

    Peter Olcott wrote:
    > "Ron Natalie" <> wrote in message
    > news:45a3e11f$0$28072$...
    >> 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?
    Simon G Best, Jan 9, 2007
    #13
  14. Peter Olcott

    Simon G Best Guest

    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?
    Simon G Best, Jan 9, 2007
    #14
  15. Peter Olcott

    Peter Olcott Guest

    "Simon G Best" <> wrote in message
    news:...
    > Peter Olcott wrote:
    >> "Ron Natalie" <> wrote in message
    >> news:45a3e11f$0$28072$...
    >>> 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?
    Peter Olcott, Jan 9, 2007
    #15
  16. Peter Olcott

    Peter Olcott Guest

    "Simon G Best" <> wrote in message
    news:...
    > 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?
    Peter Olcott, Jan 9, 2007
    #16
  17. Peter Olcott

    Ron Natalie Guest


    > 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.
    Ron Natalie, Jan 9, 2007
    #17
  18. Peter Olcott wrote:
    > "Simon G Best" <> wrote in message
    > news:...
    >> 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)
    Gianni Mariani, Jan 9, 2007
    #18
  19. Peter Olcott

    Simon G Best Guest

    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?
    Simon G Best, Jan 9, 2007
    #19
  20. Peter Olcott

    Peter Olcott Guest

    "Ron Natalie" <> wrote in message
    news:45a3fcd7$0$28100$...
    >
    >
    >> 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(){};
    Peter Olcott, Jan 9, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    665
    Matt Garman
    Apr 25, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,303
    Ivan Vecerina
    Mar 17, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    442
  4. Jeffrey Walton
    Replies:
    10
    Views:
    938
    Mathias Gaunard
    Nov 26, 2006
  5. A L
    Replies:
    12
    Views:
    2,048
Loading...

Share This Page