Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-03-26 10:34:38


Hi,

I really like Doug's extension, and I agree that a language extension is
what we really need.
I hope his gcc extension leads the way for other compilers.

Unfortunately, this only works if you have gcc :-(

A while ago I wrote a header named 'static_print.hpp' (small enough to
embedded it here, at the bottom) which I've been using to print out, via a
compiler warning, type names, values and messages from generic code.

The advantage of my method is that it doesn't require a compiler extension,
it works right out of the box.
The disadvantage is that although it gives you the information you want,
you have to look deep for it because it is embedded in a conventional
diagnostic.
So, it isn't aimed at improving the readability of error messages, only to
be able to see type names or values that would be hidden otherwise.

The best way to explain what it does is with this example:

template<class T>
struct X
{
  typedef typename T::type Y ;

  STATIC_PRINT_TYPE( The_Dependent_Type, Y ) ; // (1)

  STATIC_PRINT_INT( The_Dependent_Type_Value, Y::value ) ; // (2)

  static const bool is_it = ( Y::value == 1234 ) ;

  STATIC_PRINT_BOOL( Is_It, is_it ) ; // (3)

} ;

struct A
{
  static const int value = 1234 ;
} ;

struct B
{
  typedef A type ;
} ;

int main()
{
  X<B> x ;
}

The line marked (1) will emit a diagnostic (warning) reading something like

 "mixing pointers to different 'char' types" (depending on the compiler),

during instantiation of:

STATIC_PRINT_TYPE_The_Dependent_Type<A>

which is telling us that T::type happens to be 'A' in this particular
instantiation.

Similarly, the others lines will emit the same diagnostic but during
intantiation of:

STATIC_PRINT_INT_The_Dependent_Type_Value<1234>

STATIC_PRINT_BOOL_Is_It<1>

Well, I wrote this quick and dirty; it surely can be made more flexible and
I admit the output is as ugly as it can be, but it saved me the day more
than once...

------- START CODE ----------

#define STATIC_PRINT_TYPE(id,type) \
        template<class T> \
        struct STATIC_PRINT_TYPE_##id \
        { \
          BOOST_STATIC_CONSTANT (bool, value = ( static_cast<signed
char*>(0) \
                                                  < static_cast<unsigned
char*>(0) \
                                               ) \
                                ) ; \
        } ; \
        static const bool static_printing_type_##id =
STATIC_PRINT_TYPE_##id<type>::value

#define STATIC_PRINT_INT(id,val) \
        template<int N> \
        struct STATIC_PRINT_INT_##id \
        { \
          BOOST_STATIC_CONSTANT (bool, value = ( static_cast<signed
char*>(0) \
                                                  < static_cast<unsigned
char*>(0) \
                                               ) \
                                ) ; \
        } ; \
        static const bool static_printing_int_##id =
STATIC_PRINT_INT_##id<val>::value

#define STATIC_PRINT_BOOL(id,val) \
        template<bool B> \
        struct STATIC_PRINT_BOOL_##id \
        { \
          BOOST_STATIC_CONSTANT (bool, value = ( static_cast<signed
char*>(0) \
                                                  < static_cast<unsigned
char*>(0) \
                                               ) \
                                ) ; \
        } ; \
        static const bool static_printing_bool_##id =
STATIC_PRINT_BOOL_##id<val>::value

------- END CODE ----------

Opinions?

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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