bitfield and initialization list

M

mc

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
 
G

Gert-Jan de Vos

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.
 
A

Andrey Tarasevich

mc said:
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) {}
};
 
J

James Kanze

mc wrote:
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).
 
A

Andrey Tarasevich

James said:
<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).
 
M

mc

Andrey Tarasevich said:
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).

Thank you all. I'll just add a constructor and make use a class instead of
a struct (which are classes to some extent)
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top