Why shall a non-const reference not be bound to a bit-field?

Discussion in 'C++' started by Lighter, Jul 6, 2006.

  1. Lighter

    Lighter Guest

    On page 163 of In the C++ standard document(9.6 Bit-fields), I find
    three rules on bit-fields:

    Rule 1, "A bit-field shall not be a static member."

    Rule 2, "A non-const reference shall not be bount to a bit-field"

    Rule 3, "Note: if the initializer for a reference of type const T& is
    an lvalue that refers to a bit-field, the reference is bound to a
    temporary initialized to hold the value of the bit-field; the reference
    is not bound to the bit-field directly."

    Visual Studio 2005, however, can correctly compile and run the
    following code fragment:

    typedef int BIT;

    struct BITSET
    {
    BIT a : 1;
    BIT b : 1;
    BIT c : 1;
    BIT d : 5;
    };

    class Test
    {
    public:
    static BITSET m; // Violation of the Rule 1
    };

    BITSET Test::m = {0};

    int main()
    {
    BITSET a = {1, 0, 1};
    BITSET& b = a; // Violation of the Rule 2


    const BITSET& c = a;
    a.a = 0; // After this statement, c.a is also set to 0. Violation of
    the rule 3
    }

    Maybe someone will say: "They are just that Microsoft doesn't abide by
    the C++ standard", but what I want know is why the C++ standard
    committee made such restrictions. I cannot find enough motivation for
    the C++ standard committee to do like this. I think VS 2005 did right
    to break these rules.

    If you know the whys, please tell me. Thanks in advance.
    Lighter, Jul 6, 2006
    #1
    1. Advertising

  2. * Lighter:
    > On page 163 of In the C++ standard document(9.6 Bit-fields), I find
    > three rules on bit-fields:
    >
    > Rule 1, "A bit-field shall not be a static member."
    >
    > Rule 2, "A non-const reference shall not be bount to a bit-field"
    >
    > Rule 3, "Note: if the initializer for a reference of type const T& is
    > an lvalue that refers to a bit-field, the reference is bound to a
    > temporary initialized to hold the value of the bit-field; the reference
    > is not bound to the bit-field directly."
    >
    > Visual Studio 2005, however, can correctly compile and run the
    > following code fragment:
    >
    > typedef int BIT;
    >
    > struct BITSET
    > {
    > BIT a : 1;
    > BIT b : 1;
    > BIT c : 1;
    > BIT d : 5;
    > };
    >
    > class Test
    > {
    > public:
    > static BITSET m; // Violation of the Rule 1
    > };
    >
    > BITSET Test::m = {0};
    >
    > int main()
    > {
    > BITSET a = {1, 0, 1};
    > BITSET& b = a; // Violation of the Rule 2
    >
    >
    > const BITSET& c = a;
    > a.a = 0; // After this statement, c.a is also set to 0. Violation of
    > the rule 3
    > }
    >
    > Maybe someone will say: "They are just that Microsoft doesn't abide by
    > the C++ standard", but what I want know is why the C++ standard
    > committee made such restrictions. I cannot find enough motivation for
    > the C++ standard committee to do like this. I think VS 2005 did right
    > to break these rules.


    The above code does not demonstrate any rule breaking.

    A static bit-field would be like

    struct Foo { static int a : 1; };

    And so on -- please post your validation results with the code changed
    to use actual bitfields, not structs.


    > If you know the whys, please tell me. Thanks in advance.


    For the why's of the standard, please ask in [comp.std.c++].

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 6, 2006
    #2
    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. John Ky
    Replies:
    9
    Views:
    435
    John Ky
    Feb 23, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,834
    Smokey Grindel
    Dec 2, 2006
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. George2
    Replies:
    3
    Views:
    1,271
    James Kanze
    Dec 14, 2007
  5. George2
    Replies:
    10
    Views:
    594
    Pete Becker
    Dec 17, 2007
Loading...

Share This Page