6

Visual C++ has #pragma message that outputs a string into compiler output. Now I have a factory:

template<class Type>
CComPtr<Type> CreateComObject()
{
   CComPtr<Type> newObject( new CComObject<Type> );
   //do some tuning to the object
   return newObject;
}

and I want to output the size of class that is passed to new (namely sizeof( CComObject<Type> ) into the compiler output. Looks like #pragma message only accepts strings.

How can I output a compile-time numeric constant?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979

1 Answers1

7

If I understood your question correctly, then I think you can do this:

template<size_t size> 
struct overflow{ operator char() { return size + 256; } }; //always overflow
//if you doubt, you can use UCHAR_MAX +1 instead of 256, to ensure overflow.

template<class Type>
CComPtr<Type> CreateComObject()
{
   CComPtr<Type> newObject( new CComObject<Type> );
   char(overflow<sizeof(CComObject<Type>)>());
   return newObject;
}

The value of sizeof(CComObject<Type>) will be printed as warning messages during compilation.


See this small demo : http://www.ideone.com/Diiqy

Look at these messages (from the above link):

prog.cpp: In member function ‘overflow::operator char() [with unsigned int size = 4u]’:
prog.cpp: In member function ‘overflow::operator char() [with unsigned int size = 12u]’:
prog.cpp: In member function ‘overflow::operator char() [with unsigned int size = 400u]’:

In Visual Studio, you can see these messages in the Build Output tab; it may not appear in Error List > Warnings tab.


The idea is taken from my another solution:

Calculating and printing factorial at compile time in C++

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    @Nawaz, +1 good answer, But will it give compilation warning consistently for all compilers ? (can't it be an error or ignored message for any other compiler ?) – iammilind Apr 25 '11 at 06:37
  • @iammilind: Since the overflow is guaranteed, then all decent compiles will generate these warning messages (in my opinion). – Nawaz Apr 25 '11 at 06:38
  • It need not overflow, if `char` is more than eight bits large. And neither gcc nor Cormeau's online compiler provide these warnings. – Dennis Zickefoose Apr 25 '11 at 07:05
  • @Dennis Zickefoose: The maximum value of `char` can be 255 which is defined by the Standard itself. So it will overflow. (Or if you're not satisfied, you can use `UCHAR_MAX +1` instead of the magic number `256`). – Nawaz Apr 25 '11 at 07:13
  • @Nawaz : The maximum value of `char` is definitely not defined by the standard, only the size is (`1`). – ildjarn Apr 27 '11 at 10:08
  • @ildjarn: Its defined by the C Standard. And C++ follows it. – Nawaz Apr 27 '11 at 10:14
  • @Nawaz : No, the C standard says that the value is "*equal or greater in magnitude (absolute value) to those shown, with the same sign*", so the maximum value of `char` is *at least* `255`, not exactly `255`. – ildjarn Apr 27 '11 at 10:50
  • @ildjarn: If that is so (though I've to see it again), then `UCHAR_MAX +1` can ensure the overflow. – Nawaz Apr 27 '11 at 10:56