std::bitset in union

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • felixnielsen@hotmail.com

    #1

    std::bitset in union

    I would wery much like this to work:

    @code start
    #include <iostream>
    #include <bitset>
    const unsigned short size = 2; // 2^<unsigned int> //
    union V {
    char c[(size*size*size )/8];
    std::bitset<siz e*size*size> b;
    } v;
    @code end

    The compiler wont accept it and i cant accept that ;-)
    A far as im concersed a bitset is just a number of bits and it shouldnt
    be a problem, however it is and im in great need of a solution.

    compiler error: "member std::bitset<8u> V::b' with constuctor not
    allowed in union"

  • Thomas Tutone

    #2
    Re: std::bitset in union

    felixniel...@ho tmail.com wrote:[color=blue]
    > I would wery much like this to work:
    >
    > @code start
    > #include <iostream>
    > #include <bitset>
    > const unsigned short size = 2; // 2^<unsigned int> //
    > union V {
    > char c[(size*size*size )/8];
    > std::bitset<siz e*size*size> b;
    > } v;
    > @code end
    >
    > The compiler wont accept it and i cant accept that ;-)
    > A far as im concersed a bitset is just a number of bits and it shouldnt
    > be a problem, however it is and im in great need of a solution.
    > compiler error: "member std::bitset<8u> V::b' with constuctor not
    > allowed in union"[/color]

    Why don't you explain why you need a union. Unions are rarely needed
    in C++, in that generally you can substitute some C++ idiom instead.
    So, let us know, and I bet we can find you a solution.

    Best regards,

    Tom

    Comment

    • Bo Persson

      #3
      Re: std::bitset in union


      <felixnielsen@h otmail.com> skrev i meddelandet
      news:1140034170 .027456.12410@g 14g2000cwa.goog legroups.com...[color=blue]
      >I would wery much like this to work:
      >
      > @code start
      > #include <iostream>
      > #include <bitset>
      > const unsigned short size = 2; // 2^<unsigned int> //
      > union V {
      > char c[(size*size*size )/8];
      > std::bitset<siz e*size*size> b;
      > } v;
      > @code end
      >
      > The compiler wont accept it and i cant accept that ;-)[/color]

      Too bad. :-)
      [color=blue]
      > A far as im concersed a bitset is just a number of bits and it
      > shouldnt
      > be a problem, however it is and im in great need of a solution.[/color]

      It's an object. The internal implementation is not specified.
      [color=blue]
      >
      > compiler error: "member std::bitset<8u> V::b' with constuctor not
      > allowed in union"[/color]

      Exactly.

      However,
      if you keep the size within moderate ranges, there is a constructor
      bitset(unsigned long) that sets the bits from an unsigned long. There
      is also the opposite conversion bitset::to_ulon g(), that gets you as
      many bits as will fit.

      As the size of an unsigned long is not fixed (but at least 32 bits) it
      might solve just some of your problems.


      Bo Persson


      Comment

      • Marcus Kwok

        #4
        Re: std::bitset in union

        felixnielsen@ho tmail.com wrote:[color=blue]
        > I would wery much like this to work:
        >
        > @code start
        > #include <iostream>
        > #include <bitset>
        > const unsigned short size = 2; // 2^<unsigned int> //
        > union V {
        > char c[(size*size*size )/8];
        > std::bitset<siz e*size*size> b;
        > } v;
        > @code end
        >
        > The compiler wont accept it and i cant accept that ;-)
        > A far as im concersed a bitset is just a number of bits and it shouldnt
        > be a problem, however it is and im in great need of a solution.
        >
        > compiler error: "member std::bitset<8u> V::b' with constuctor not
        > allowed in union"[/color]

        I found this thread:


        In Ron Ruble's post, he quotes the standard (section 9.5):

        "An object of a class with a non-trivial constructor (12.1), a
        non-trivial destructor (12.4), or a non-trivial copy constructor
        (13.5.3, 12.8) cannot be a member of a union"

        --
        Marcus Kwok

        Comment

        • JetSnaiL

          #5
          Re: std::bitset in union

          > felixnielsen@ho tmail.comwrote:
          I would wery much like this to work:[color=blue]
          >
          > @code start
          > #include <iostream>
          > #include <bitset>
          > const unsigned short size = 2; // 2^<unsigned int> //
          > union V {
          > char c[(size*size*size )/8];
          > std::bitset<siz e*size*size> b;
          > } v;
          > @code end
          >
          > The compiler wont accept it and i cant accept that ;-)
          > A far as im concersed a bitset is just a number of bits and it[/color]
          shouldnt[color=blue]
          > be a problem, however it is and im in great need of a solution.
          >
          > compiler error: "member std::bitset<8u> V::b' with constuctor[/color]
          not[color=blue]
          > allowed in union"[/color]

          Dear Sir, C++ union is a little bit too useless because it supports
          only POD types. In your case you have std::bitset which is not a POD
          type. There are many solutions, but the best I can recommend is to
          use "boost::variant ". Variant template in boost is a safe, generic,
          stack-based discriminated union container, offering a simple solution
          for manipulating an object from a heterogeneous set of types in a
          uniform manner. That's what you need. Read more at


          Wish best regards,
          Vladislav Lazarenko.

          Comment

          • JetSnaiL

            #6
            Re: std::bitset in union

            > felixnielsen@ho tmail.comwrote:
            I would wery much like this to work:[color=blue]
            >
            > @code start
            > #include <iostream>
            > #include <bitset>
            > const unsigned short size = 2; // 2^<unsigned int> //
            > union V {
            > char c[(size*size*size )/8];
            > std::bitset<siz e*size*size> b;
            > } v;
            > @code end
            >
            > The compiler wont accept it and i cant accept that ;-)
            > A far as im concersed a bitset is just a number of bits and it[/color]
            shouldnt[color=blue]
            > be a problem, however it is and im in great need of a solution.
            >
            > compiler error: "member std::bitset<8u> V::b' with constuctor[/color]
            not[color=blue]
            > allowed in union"[/color]

            Dear Sir, C++ union is a little bit too useless because it supports
            only POD types. In your case you have std::bitset which is not a POD
            type. There are many solutions, but the best I can recommend is to
            use "boost::variant ". Variant template in boost is a safe, generic,
            stack-based discriminated union container, offering a simple solution
            for manipulating an object from a heterogeneous set of types in a
            uniform manner. That's what you need. Read more at


            Wish best regards,
            Vladislav Lazarenko.

            Comment

            • felixnielsen@hotmail.com

              #7
              Re: std::bitset in union

              I have done some thingking and i now realize that i dont actually need
              this, however i still think it could be usefull when manipulating bit
              in wery large numbers.

              Otherwise im thankfull for your help.

              Regards
              Zacariaz

              Comment

              • Jacek Dziedzic

                #8
                Re: std::bitset in union

                felixnielsen@ho tmail.com wrote:[color=blue]
                > I have done some thingking and i now realize that i dont actually need
                > this, however i still think it could be usefull when manipulating bit
                > in wery large numbers.
                >
                > Otherwise im thankfull for your help.[/color]

                As a side note -- if you had planned to use this union
                to 'convert' the bitset to char[] or vice versa, you'd
                better be warned that it's illegal to do that using a
                union.

                HTH,
                - J.

                Comment

                Working...