c++ - Print fully evaluated result of #define during compilation using #pragma message() -


Using a #pragma message, I have a quick question about printing evaluation values ​​of #defines, I used Visual Studio 2008 I am using MSVC ++ Below is a simple example:

  #define __STR2___ (x) #x #define___r1___ (x) __STR2__ (x)) #defined WIDTH 10 #defined HEIGHT 10 # The defined area (WIDTH * HEIGHT) #pragma message ("The area is:" __STR1 __ (ARA))  

Now when I compile, I get the following output:

  & gt; The area is: (10 * 10)  

This is not what I need. Is there a way to print #define expression evaluation, which I found:

  & gt; The area is: 100  

It is probably not possible during the compilation, eventually I would like to cause the compiler error if the evaluation value is very large. I.e.

  #if (region> 1000) #pragma message (__default ERROR__) #endif  

use some of my # definitions sizeof ( ) which I believe is the cause of issues in evaluating the conditions - but this is a problem for the future!

I saw the following post which is defined as #define as a value, and there is not a combination of other # definitions.

won the preprocessor 'can not do the math for you, it can only move the tokens and Can expand macros in text mode.

If you want to calculate that value during the compilation, then you should go for the constexpr properly, it will prompt the compiler to compute at compile-time)

  #include & lt; Iostream & gt; #defined WIDTH 10 #defitted HEIGHT10 template & lt; Int a, int b & gt; Constexpr int getArea () {static_assert (a * b & lt; 1000, "area is too big"); Return * b; } Const int area = getArea & lt; WIDTH, HEIGHT> (); Int main (zero) {std :: cout & lt; & Lt; Area; }  

will check for the area if it is too large.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -