Article Discussion
Safe Labels in C++
Summary: The need to write efficient and high-performance programs in C++ make it desirable to be able to manipulate bits and groups of bits easily, efficiently, and safely. C++ provides out-of-the-box tools to accomplish the first two goals with its bitwise operations, but it does so at the expense of the third objective, safety. This article presents a solution to the problem of constraining bit operations to allow only safe and legitimate ones, and turn all invalid bit manipulations into compile-time errors. Best of all, the syntax of bit operations remains unchanged, and the code working with bits does not need to be modified, except possibly to fix errors that had as yet remained undetected.
21 posts on 2 pages.      
« Previous 1 2 Next »
The ability to add new comments in this discussion is temporarily disabled.
Most recent reply: December 27, 2008 11:12 PM by M
Nemanja
Posts: 40 / Nickname: ntrif / Registered: June 30, 2004 1:10 AM
Re: Safe Labels in C++
October 4, 2007 4:15 AM      
> It does not say anything, really, just a vague reference
> to gcc being slower in C++ than in C. Which says nothing.
> And it does not talk about 'equivalent programs'.

"It turned out that compiling a piece of C code with g++ would give you worse code. It shouldn't have made a difference, but it did."

> All these are programs on top of the kernel, so they are
> not part of the O/S, they are part of the O/S
> distribution. There is a difference.

:)

O/S != kernel;
O/S = kernel + shell + various_other_user_space_services;
}
Roman
Posts: 1 / Nickname: buyvicodin / Registered: October 4, 2007 10:24 PM
Re: Safe Labels in C++
October 5, 2007 3:29 AM      
0000
Achilleas
Posts: 98 / Nickname: achilleas / Registered: February 3, 2005 2:57 AM
Re: Safe Labels in C++
October 5, 2007 5:13 AM      
> > It does not say anything, really, just a vague
> reference
> > to gcc being slower in C++ than in C. Which says
> nothing.
> > And it does not talk about 'equivalent programs'.
>
> "It turned out that compiling a piece of C code with g++
> would give you worse code. It shouldn't have made a
> difference, but it did."

I never believed that. They never showed any code.

>
> > All these are programs on top of the kernel, so they
> are
> > not part of the O/S, they are part of the O/S
> > distribution. There is a difference.
>
> :)
>
> O/S != kernel;
> O/S = kernel + shell + various_other_user_space_services;
> }

In the context of programming languages, the only part of an O/S distribution that count as an O/S is the kernel.

You can't say, for example, that Internet Explorer is part of the O/S, because it is a normal application that could have been written in any language.
Max
Posts: 18 / Nickname: mlybbert / Registered: April 27, 2005 11:51 AM
Why not C bitfields?
October 10, 2007 1:56 PM      
I had a hard time reading the article, because I felt that it solves an artificial problem. I can't see why a C bitfield ( http://msdn2.microsoft.com/en-us/library/yszfawxh.aspx for instance) wouldn't solve the problem:

struct Cat_state
{
unsigned int SLEEPING : 1;
unsigned int PURRING : 1;
unsigned int PLAYING : 1;
}

struct Dog_state
{
...
}

Cat_state c;
c.SLEEPING = 1;

Dog_state d;
d.CHEWING = 1;
d.BARKING = 1;

if (c.SLEEPING)
{
...
};
Miguel
Posts: 1 / Nickname: mtadeunet / Registered: November 12, 2007 2:10 AM
Re: Why not C bitfields?
November 12, 2007 10:36 AM      
I read a book that noted just that(can remember which), but I picked the example described there and added a few features:
// from the book
template<unsigned long N>
struct binary
{
static const unsigned int digit = N % 10;
BOOST_STATIC_ASSERT(digit == 0 || digit == 1);

static unsigned long const value = binary<N/10>::value * 2 + digit;
};

template<>
struct binary<0>
{
static unsigned int const value = 0;
};

// from me...
template<unsigned long N, unsigned long S>
struct binary_shift_left : binary<N>
{
static unsigned long const value = (binary<N>::value << S);
};

template<unsigned long N, unsigned long S>
struct binary_shift_right : binary<N>
{
static unsigned long const value = (binary<N>::value >> S);
};

template<unsigned long N, unsigned long P = 0>
struct flag : binary_shift_left<N, P> { };

template<unsigned long P>
struct bit_flag : flag<1, P>{ };

// usage
int main(int argc, char *argv[])
{
unsigned long flags = 0x00;

flags = binary<1010101010>::value;

flags |= bit_flag< 1 >::value;
flags |= bit_flag< 2 >::value;
flags |= bit_flag< 3 >::value;
flags |= bit_flag< 4 >::value;
flags |= bit_flag< 5 >::value;
flags |= bit_flag< 6 >::value;
flags |= bit_flag< 7 >::value;
flags |= bit_flag< 8 >::value;

flags |= flag<11, 9>::value | flag<101, 5>::value;

cout << binary<1111>::value << endl;

return EXIT_SUCCESS;
}


It's to be noticed that recent compilers will resolve the templates and the code remains as optimized as in C.
M
Posts: 1 / Nickname: ma740988 / Registered: December 27, 2008 5:02 PM
Re: Safe Labels in C++
December 27, 2008 11:12 PM      
Interesting to say the least. I'll need to read the article again but assume CAT_PURRING required 4 bits. To provide coverage across a range, I would end up with:

BIT_CONST( Cat_state, CAT_SLEEPING, 1 );
BIT_CONST( Cat_state, CAT_PURRING, 2 );
BIT_CONST( Cat_state, CAT_PLAYING, 6 );

i.e CAT_PLAYING will start at location 6?
21 posts on 2 pages.
« Previous 1 2 Next »