|
Boost Users : |
From: Erik (esigra_at_[hidden])
Date: 2008-03-19 06:02:15
John Maddock skrev:
> Erik wrote:
>
>> John Maddock skrev:
>>
>>> Jean-Francois Bastien wrote:
>>>
>>>>> We have this really neat and simple macro in our project:
>>>>> #define compile_assert(x) typedef bool
>>>>> COMPILE_ASSERT[(x) ? 1 : -1]
>>>>>
>>>>> Although it does what we need and serves us well, I
>>>>> tried to replace
>>>>> it with BOOST_STATIC_ASSERT. Unfortunately this resulted
>>>>> in a lot of
>>>>> ugly warnings throughout the compile:
>>>>> warning: use of old-style cast
>>>>>
>>>>>
>>>> There's the following comment in the static assert header:
>>>>
>>>> // Note that the argument to the assert is explicitly cast to bool
>>>> using old-
>>>> // style casts: too many compilers currently have problems with
>>>> static_cast
>>>> // when used inside integral constant expressions.
>>>>
>>>> The solution would be to add even more defines to use old style or
>>>> static casts depending on which compiler is used.
>>>>
>>> Right: historically this is why the header doesn't use static_cast
>>> here.
>>>
>>> Unfortunately GCC appears to be one of the compilers that chokes if
>>> you use static_cast in an integral constant expression: testing the
>>> diff below with gcc-3.4 (cygwin) results in the tests failing (where
>>> as enabling the same static_cast useage with msvc or intel does work
>>> OK).
>>>
>>> If you anyone can test this with gcc-4.x and let me know which
>>> versions might accept this patch OK that would be very useful, I'm
>>> attaching the updated static_assert.hpp as well as the diff.
>>>
>> Here is my test.
>> 1. Execute "mkdir static_assert; cd static_assert".
>> 2. Save your attached file static_assert.hpp there.
>> 3. Create the test file prov.cc there.
>>
>> The rest of the test results follow as console output (where
>> "static_assert $ " is the prompt):
>> static_assert $ cat --number prov.cc
>> 1 #include "static_assert.hpp"
>> 2 BOOST_STATIC_ASSERT(1 < 2);
>> 3 BOOST_STATIC_ASSERT(1 > 2);
>> static_assert $ LANG="" g++-4.1.2 -Wall -Wextra -Wold-style-cast -c
>> prov.cc prov.cc:3: error: parse error in template argument list
>> prov.cc:3: error: template argument 1 is invalid
>> prov.cc:3: error: expected unqualified-id at end of input
>> static_assert $ LANG="" g++-4.2.3 -Wall -Wextra -Wold-style-cast -c
>> prov.cc prov.cc:3: error: parse error in template argument list
>> prov.cc:3: error: template argument 1 is invalid
>>
>>
>> As you can see, it fails at line 3 as it should, while not complaining
>> about line 2. It will not complain about any old-style-cast either. So
>> the test was completely successful.
>>
>
> Hardly that successful: those error messages are awful! The whole point of
> BOOST_STATIC_ASSERT is to provide semi-readable error messages - at least so
> that you know it's a static assertion failure and not some other issue
> that's the problem.
That is a good point. We actually get such a semi-readable error message
with our current compile_assert:
$ cat --number prov.cc
1 #define compile_assert(x) typedef bool COMPILE_ASSERT[(x) ? 1 : -1]
2 compile_assert(1 < 2);
3 compile_assert(1 > 2);
$ LANG="" g++-4.1.2 -Wall -Wextra -Wold-style-cast -c prov.cc
prov.cc:3: error: size of array 'COMPILE_ASSERT' is negative
$ LANG="" g++-4.2.3 -Wall -Wextra -Wold-style-cast -c prov.cc
prov.cc:3: error: size of array 'COMPILE_ASSERT' is negative
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