View Single Post
Old Feb 10th, 2008, 1:23 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,076
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Code Segfaults when compiled with optimizations, doesn't in arbitrary situations

Boost Foreach needs to know how long the sequence is. Currently there's no way of telling where your LETTERS array ends.

It can iterate over C-style strings. However, your { 'A', 'B' } is not null-terminated. So the following should also work, since it now knows how large your array is:

const char LETTERS[] = { 'A', 'B', '\0' };

BOOST_FOREACH( char letter, LETTERS ) {
    // ...
}

This should also work:

std::string LETTERS( "AB" );

BOOST_FOREACH( char letter, LETTERS ) {
    // ...
}
Sane is online now   Reply With Quote