Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::format warning
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2010-01-15 18:18:22


El 15/01/2010 05:35 p.m., Henning Meyer escribió:
> Hello,
>
> when compiling a simple test like this:
>
> #include <boost/format.hpp>
> int main(void) {
> int c = 0;
> boost::format("%1% %2%") % (c==42) % c++;
> }
>
> with this command:
> $ c++ -c -O -Wall -o ftest.o ftest.cc
>
> I get this warning:
> ftest.cc: In function ‘int main()’:
> ftest.cc:4: warning: operation on ‘c’ may be undefined
>
> What did I do wrong?
>

Its implementation defined whether the evaluation of c==42 will happen
before or after the evaluation of c++. I am happy to see a compiler that
does emit a warning on this kind of code. Simply modify your code like this
    boost::format("%1% %2%") % (c==42) % c;
    c++;
or this
    c++;
    boost::format("%1% %2%") % (c==42) % c;
depending on what you actually intended.

More on the subject here:
http://talesofcpp.blogspot.com/2009/10/episode-ten-natural-order-of-things.html

K-ballo.-


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net