Which is the best way for holding flags?

L

loquak

Greets.
I wonder which way would be the best for holding some state flags in terms
of memory use and efficiency.

a)

struct FLAGS
{
bool flag1 : 1;
bool flag2 : 1;
bool flag3 : 1;
....
}
FLAGS flags;

b)

enum FLAGS
{
flag1,
flag2,
flag3
....
}
std::bitset<FLAGS> flags;

c)

bool flag1;
bool flag2;
bool flag3;
....

Which way would you prefer?
 
J

Jonathan Turkanis

loquak said:
Greets.
I wonder which way would be the best for holding some state flags in terms
of memory use and efficiency.

a)

struct FLAGS
{
bool flag1 : 1;
bool flag2 : 1;
bool flag3 : 1;
...
}

I hope it will not offend you if I say: Yuck.
FLAGS flags;

b)

enum FLAGS
{
flag1,
flag2,
flag3
...
}
std::bitset<FLAGS> flags;

This is wrong: std::bitset takes a (non-type) std::size_t parameter.
c)

bool flag1;
bool flag2;
bool flag3;
...

This wastes space.
Which way would you prefer?

d)

namespace flags {
const int flag1 = 1;
const int flag2 = flag1 << 1;
const int flag3 = flag2 << 1;
....
}

int flags;

Jonathan
 
J

Jonathan Turkanis

Jonathan Turkanis said:
namespace flags {
const int flag1 = 1;
const int flag2 = flag1 << 1;
const int flag3 = flag2 << 1;
....
}

int flags;

Whoops! I meant

int flags_;

Jonathan
 
G

Gianni Mariani

loquak said:
Greets.
I wonder which way would be the best for holding some state flags in terms
of memory use and efficiency.

- here, memory use and efficiency are 2 very different things.

It is undoubtedly faster to read and write bool or perhaps char values
than it is to read an write individual bits.

You need to ask yourself, is memory use more important than performance.

Now, if you have such large numbers of bits, such that you're trashing
cache all the time, you might find that a compact format is more
interesting.

Hence, I would probably choose a) if size was an issue, or c) if
performance was more important.
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Jonathan said:
Whoops! I meant

int flags_;

Jonathan

What does the trailing underscore do? Or is it just a notation thing? Just
curious.
 
J

Jonathan Turkanis

Matthias Käppler said:
What does the trailing underscore do? Or is it just a notation thing? Just
curious.

I didn't want the namespace and the variable holding the flags to have the same
name. Usually the variable consisting of a combination of flags will be a data
member of some class; I usually call it "flags_".

Jonathan
 
J

Jonathan Turkanis

PKH said:
Jonathan Turkanis said:
Whoops! I meant

int flags_;

Jonathan

One technique I read in the faq that is nice if you have more flags than can
fit into an integer datatype :
http://www.parashift.com/c++-faq-lite/ctors.html [10.17] What is the "Named
Parameter Idiom"?

David Abrahams and Daniel Wallin have a nice library which allows name
parameters, using syntax like:

Dog* d = new Dog(age = 3, weight = 45.1);

It was just reviewed for inclusion in Boost; the result is pending.

I don't really see the connection between this and bit flags, though.

Jonathan
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top