Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2003-06-15 16:28:46


"Robert Ramey" <ramey_at_[hidden]> wrote in message
news:01C33327.14580600_at_226-146.adsl2.netlojix.net...
> Hmmmm - I never imagined that something like this would be so problematic.
>
> For now with my VC 7.0 compiler I can use the following and it gives
> me almost exactly what I need. The warning message points exactly
> to the place in my code where I have invoked it - just like
BOOST_STATIC_ASSERT.
>
> I would hope something like this could be boostified so that I could use
it outside
> of a function.
>
> template<bool>
> struct warning
> {
> typedef int type;
> };
>
> template<>
> struct warning<true>
> {
> typedef char type;
> };
>
> #define BOOST_STATIC_WARNING(B) \
> { \
> char x = (warning<B>::type)0xffff; \
> }
>
Played with, it works on Intel C++ 7 and MSVC 6 (two warnings issued).

It doesn't work for BC++B and needs change, e.g.:

#ifdef __BORLANDC__
template<bool>
struct warning { typedef int& type };

template<>
struct warning<true> { typedef int type; };

#define BOOST_STATIC_WARNING(B) \
{ \
    warning<B>::type compile_time_warning_issued = 0; \
    compile_time_warning_issued = compile_time_warning_issued; \
}
#endif

(It produces warning "temporary used to initialize
'compile_time_warning_issued'.)

It would not work if
  #pragma warn -8028
is defined.

I thought about preprocessor based solution (for compilers supporting
#pragma message):

------------
file test.cpp:

#include <boost/preprocessor.hpp>

#define BOOST_STATIC_WARNING(x) \
    BOOST_PP_STRINGIZE( BOOST_PP_CAT( BOOST_PP_CAT(warning,
BOOST_PP_BOOL(x) ), .hpp ) )

int main()
{
  #define BOOST_STATIC_WARNING_TEXT "detailed warning description"
  #include BOOST_STATIC_WARNING(0)
}

------------
file warning0.hpp:

#ifndef BOOST_STATIC_WARNING_TEXT
#error You must define BOOST_STATIC_WARNING_TEXT each time before using
BOOST_STATIC_WARNING
#endif
#ifdef _MSC_VER
# pragma message ("COMPILE TIME WARNING: " BOOST_STATIC_WARNING_TEXT)
#endif
#undef BOOST_STATIC_WARNING_TEXT

------------
file warning1.hpp:

#ifndef BOOST_STATIC_WARNING_TEXT
#error You must define BOOST_STATIC_WARNING_TEXT each time before using
BOOST_STATIC_WARNING
#endif
#undef BOOST_STATIC_WARNING_TEXT
------------

Output (Intel C++ 7.0 plugged in MSVC 6 IDE, MSVC 6 as well):
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
COMPILE TIME WARNING: detailed warning description
Linking...
link: executing 'C:\PROGRA~1\Microsoft Visual Studio\VC98\Bin\link.exe'

test.exe - 0 error(s), 1 warning(s)

It cannot be used to generate warnings for template instantiations etc.

/Pavel


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