Boost logo

Boost :

Subject: Re: [boost] suggestion on assertion macros
From: Mateusz Loskot (mateusz_at_[hidden])
Date: 2010-03-14 11:45:33


Roland Bock wrote:
> Mateusz Loskot wrote:
>>> Currently I get compiler warnings about my_param being unused when
>>> compiling with NDEBUG defined.
>>>
>>
>> Yes, that is a problem though of different nature than categories of
>> assertions.
>>
>>
>>> I'd be glad to get rid of them. And I
>>> certainly do not want to have my code sprinkled with
>>>
>>> #ifdef NDEBUG
>>> void my_method(const int&)
>>> #else
>>> void my_method(const int& my_param)
>>> #endif
>>> {
>>> assert(my_param);
>>> }
>>>
>>
>> AFAIK, this is an issue of particular implementation you use.
>> The assert macro from GNU C Library wraps its argument
>> cast to void if NDEBUG is defined. No warning is issued.
>>
>> Best regards,
>>
>
> Hmm. I use gcc (4.2.4) and I get the warning (-Wall -Wextra).

I forgotten about -Wextra option and now GCC 4.4.1 reports the same
worning, indeed. I was wrong, the assert macro is not that clever as I
suggested

$ g++ -E -pedantic -Wall -Wextra -DNDEBUG test.cpp
int main()
{
    int* p = 0;
    (static_cast<void> (0)); // assert macro
}

$ g++ -pedantic -Wall -Wextra -DNDEBUG test.cpp
test.cpp: In function ‘int main()’:
test.cpp:4: warning: unused variable ‘p’

Best regards,

-- 
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk