bitfield and initialization list

Discussion in 'C++' started by mc, Dec 15, 2008.

  1. mc

    mc Guest

    Hi all,

    Is it possible to initialize a bitfield with an initialization list? If
    yes, what is the syntax?

    typedef struct
    {
    unsigned a : 1;
    unsigned b : 1;
    } bf;

    class Foo
    {
    Foo() : bf(false, false)
    {
    }
    };
    This and other attempts were unsuccessful.


    Thank you in advance.

    Regards,

    mc
    mc, Dec 15, 2008
    #1
    1. Advertising

  2. On Dec 15, 7:21 pm, "mc" <> wrote:
    > Hi all,
    >
    > Is it possible to initialize a bitfield with an initialization list?  If
    > yes, what is the syntax?
    >
    > typedef struct
    > {
    > unsigned a : 1;
    > unsigned b : 1;
    >
    > } bf;
    >
    > class Foo
    > {
    >    Foo() : bf(false, false)
    >   {
    >   }};


    You are close, your bf struct is just another struct which can be
    initialized from its constructor:

    struct bitfield
    {
    bitfield(unsigned a, unsigned b) :
    a(a), b(b)
    {
    }

    unsigned a : 1;
    unsigned b : 1;
    };

    class Foo
    {
    Foo() : bf(false, false)
    {
    }

    bitfield bf;
    };

    Alternatively: make your bitfields direct members of the Foo class.
    Gert-Jan de Vos, Dec 15, 2008
    #2
    1. Advertising

  3. mc wrote:
    >
    > Is it possible to initialize a bitfield with an initialization list? If
    > yes, what is the syntax?
    >
    > typedef struct
    > {
    > unsigned a : 1;
    > unsigned b : 1;
    > } bf;
    >
    > class Foo
    > {
    > Foo() : bf(false, false)
    > {
    > }
    > };
    > This and other attempts were unsuccessful.


    You need to state more precisely what is it your are trying to do. 'bf'
    is just a typedef name, not a member or base name of 'Foo', which means
    that using it in the constructor initializer list the way it is used in
    the above example simply makes no sense whatsoever.

    Bitfields are no different from any other data members. They can be
    initialized in just like any other data members. For a standalone 'bf'
    object (as declared above) it can be done with an aggregate initializer

    bf s = { false, true };

    However, aggregate initializer cannot be used in constructor initializer
    lists, which means that if in class 'Foo' you have a data member of type
    'bf' (as declared above) the only option you have is to use the '()'
    initializer

    class Foo {
    bf s;

    Foo() : s() {}
    };

    This will set all members of 's' to '0'.

    But if you want to use specific values for each bitfield in 'bf', you'll
    have to provide a user-declared constructor that does what you need. And
    you'll have to give your struct a real name (why would you use such a
    typedef in C++ anyway)

    struct bf {
    unsigned a : 1;
    unsigned b : 1;

    bf(bool a, bool b) : a(a), b(b) {}
    };

    class Foo {
    bf s;

    Foo() : s(false, true) {}
    };

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Dec 15, 2008
    #3
  4. mc

    James Kanze Guest

    On Dec 15, 8:31 pm, Andrey Tarasevich <>
    wrote:
    > mc wrote:


    > > Is it possible to initialize a bitfield with an
    > > initialization list? If yes, what is the syntax?


    > > typedef struct
    > > {
    > > unsigned a : 1;
    > > unsigned b : 1;
    > > } bf;


    > > class Foo
    > > {
    > > Foo() : bf(false, false)
    > > {
    > > }
    > > };
    > > This and other attempts were unsuccessful.


    > You need to state more precisely what is it your are trying to
    > do. 'bf' is just a typedef name, not a member or base name of
    > 'Foo', which means that using it in the constructor
    > initializer list the way it is used in the above example
    > simply makes no sense whatsoever.


    <nitpicking mode=on>
    Yes and no. For historical reasons, "If the typedef declaration
    defines an unnamed class (or enum), the first typedef-name
    declared by the declaration to be that class type (or enum type)
    is used to denote the class type (or enum type) for linkage
    purposes only. So bf is sort of a class name, and would
    certainly be legal in an initializer list, provided the compiler
    could find a corresponding constructor. (Of course, since an
    unnamed class can't have a constructor, this is going to be
    difficult.)

    Still, I'd recommend anyone not concerned with writing headers
    which are shared by C and C++ to act as if this rule doesn't
    exist, and to define the class in the classical C++ fashion:

    struct BitFields
    {
    unsigned a : 1 ;
    unsigned b : 1 ;
    } ;

    Which leads to the rest of what you said (which was right on
    target).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 16, 2008
    #4
  5. James Kanze wrote:
    >>> Is it possible to initialize a bitfield with an
    >>> initialization list? If yes, what is the syntax?

    >
    >>> typedef struct
    >>> {
    >>> unsigned a : 1;
    >>> unsigned b : 1;
    >>> } bf;

    >
    >>> class Foo
    >>> {
    >>> Foo() : bf(false, false)
    >>> {
    >>> }
    >>> };
    >>> This and other attempts were unsuccessful.

    >
    >> You need to state more precisely what is it your are trying to
    >> do. 'bf' is just a typedef name, not a member or base name of
    >> 'Foo', which means that using it in the constructor
    >> initializer list the way it is used in the above example
    >> simply makes no sense whatsoever.

    >
    > <nitpicking mode=on>
    > Yes and no. For historical reasons, "If the typedef declaration
    > defines an unnamed class (or enum), the first typedef-name
    > declared by the declaration to be that class type (or enum type)
    > is used to denote the class type (or enum type) for linkage
    > purposes only. So bf is sort of a class name, and would
    > certainly be legal in an initializer list, provided the compiler
    > could find a corresponding constructor. (Of course, since an
    > unnamed class can't have a constructor, this is going to be
    > difficult.)


    Good point, but apparently, you misunderstood my intent. I wasn't really
    referring to the issue of using typedef-name in place of a class name
    (which is indeed legal), but rather to something more trivial. In the
    OP's example name 'bp' refers to one class, while the constructor
    initializer list in question belongs to a completely different class -
    'Foo'. Since 'bf' is mentioned in the constructor initializer list of
    'Foo', it is required to be either a base class of 'Foo' or a non-static
    data member of 'Foo'. Neither is the case in the OP's example, which is
    why I said it makes no sense.

    Of course, there's a possibility that the OP simply forgot to include
    the relevant definition into his short code sample. But somehow I doubt
    that deriving 'Foo' from 'bf' was his intent. And I also doubt that he
    meant to declare something as unusual as a data member named 'bf' of
    type named 'bf' in class 'Foo' (even though it is technically legal).

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Dec 16, 2008
    #5
  6. mc

    mc Guest

    "Andrey Tarasevich" <> wrote in message
    news:gi8n7k$iud$...
    > James Kanze wrote:
    >>>> Is it possible to initialize a bitfield with an
    >>>> initialization list? If yes, what is the syntax?

    >>
    >>>> typedef struct
    >>>> {
    >>>> unsigned a : 1;
    >>>> unsigned b : 1;
    >>>> } bf;

    >>
    >>>> class Foo
    >>>> {
    >>>> Foo() : bf(false, false)
    >>>> {
    >>>> }
    >>>> };
    >>>> This and other attempts were unsuccessful.

    >>
    >>> You need to state more precisely what is it your are trying to
    >>> do. 'bf' is just a typedef name, not a member or base name of
    >>> 'Foo', which means that using it in the constructor
    >>> initializer list the way it is used in the above example
    >>> simply makes no sense whatsoever.

    >>
    >> <nitpicking mode=on>
    >> Yes and no. For historical reasons, "If the typedef declaration
    >> defines an unnamed class (or enum), the first typedef-name
    >> declared by the declaration to be that class type (or enum type)
    >> is used to denote the class type (or enum type) for linkage
    >> purposes only. So bf is sort of a class name, and would
    >> certainly be legal in an initializer list, provided the compiler
    >> could find a corresponding constructor. (Of course, since an
    >> unnamed class can't have a constructor, this is going to be
    >> difficult.)

    >
    > Good point, but apparently, you misunderstood my intent. I wasn't really
    > referring to the issue of using typedef-name in place of a class name
    > (which is indeed legal), but rather to something more trivial. In the OP's
    > example name 'bp' refers to one class, while the constructor initializer
    > list in question belongs to a completely different class - 'Foo'. Since
    > 'bf' is mentioned in the constructor initializer list of 'Foo', it is
    > required to be either a base class of 'Foo' or a non-static data member of
    > 'Foo'. Neither is the case in the OP's example, which is why I said it
    > makes no sense.
    >
    > Of course, there's a possibility that the OP simply forgot to include the
    > relevant definition into his short code sample. But somehow I doubt that
    > deriving 'Foo' from 'bf' was his intent. And I also doubt that he meant to
    > declare something as unusual as a data member named 'bf' of type named
    > 'bf' in class 'Foo' (even though it is technically legal).
    >
    > --
    > Best regards,
    > Andrey Tarasevich


    Thank you all. I'll just add a constructor and make use a class instead of
    a struct (which are classes to some extent)
    mc, Dec 16, 2008
    #6
    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. Erik Cato

    bitfield and enum, is this legal?

    Erik Cato, Feb 23, 2004, in forum: C Programming
    Replies:
    8
    Views:
    6,275
    Barry Schwarz
    Feb 27, 2004
  2. Replies:
    6
    Views:
    459
    Ron Natalie
    Dec 11, 2005
  3. toton
    Replies:
    5
    Views:
    934
    Victor Bazarov
    Sep 28, 2006
  4. aaragon
    Replies:
    2
    Views:
    617
    James Kanze
    Nov 2, 2008
  5. Stanley Rice

    bitfield in struct and padding

    Stanley Rice, Nov 10, 2011, in forum: C Programming
    Replies:
    6
    Views:
    419
    Keith Thompson
    Nov 10, 2011
Loading...

Share This Page