locked
Forcing a preprocessor error from within a macro in VC++ RRS feed

  • Question

  • Here is a C++ preprocessing challenge for VC++: how can one generate a preprocessing error from within a C++ macro in VC++ ?

    Normally one can generate a preprocessing error using the #error construct. It is well known that one can not generate preprocessing constructs, including an #error construct, from within a C++ macro, so this method is not available. Furthermore passing the wrong number of parameters to a macro only generates a warning, by default, with VC++. Nor does surrounding the macro definition with a #pragma warning changing the warning to an error do anything.

    So is there a reliable way ?


    • Edited by eldiener Saturday, July 9, 2011 5:48 PM Corrected title
    Saturday, July 9, 2011 4:31 PM

Answers

  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    Similarly you might be able to utilize the static_assert concept, e.g.

     

    #define MY_MACRO() static_assert(0, "MY_MACRO caused error.")
    

     


    This is a good idea but I need a solution for older versions of VC++ which do not have C++0x support. Like static_assert my solution must produce a compile-time error.

    I think Pavel A's suggestion of C_ASSERT is a compile time assertion check that should be available on some older versions of VC++.  If you aren't already using the Windows functionality where it is defined (winnt.h), you should be able to role your own version of the concept, e.g.:

    #define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]
    

    Kind of ugly, but maybe it will do until a better solution is found.

    • Marked as answer by Rob Pan Monday, July 18, 2011 9:18 AM
    Thursday, July 14, 2011 2:49 PM
  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    Similarly you might be able to utilize the static_assert concept, e.g.

    #define MY_MACRO() static_assert(0, "MY_MACRO caused error.")
    
    

     


    The static_assert is not for macroes, it works during compiling time, which is after the macro decoding proceed.
    - I like windows OSs and wanna work for them - We r the coders, U will be reprogrammed, resistance cause unhandled exceptions... - Recent code Lab: http://cppwindowsapi.codeplex.com/
    • Marked as answer by Rob Pan Monday, July 18, 2011 9:17 AM
    Thursday, July 14, 2011 5:43 PM

All replies

  • eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate a  preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message


    Igor Tandetnik

    Saturday, July 9, 2011 6:16 PM
  • eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate a  preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message


    Igor Tandetnik

    Why do you think that will force a preprocessing error ? That just creates output without forcing a preprocessing error.

    I should have also specified that I want to force a preprocessing error without any preprocessing output created.

    Saturday, July 9, 2011 7:03 PM
  • eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate  a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    Why do you think that will force a preprocessing error ? That just  creates output without forcing a preprocessing error.

    That would create an error when the macro is actually used. Isn't that  what you wanted? I suppose I don't understand your use case.


    Igor Tandetnik

    Saturday, July 9, 2011 7:55 PM
  • eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate  a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    Why do you think that will force a preprocessing error ? That just  creates output without forcing a preprocessing error.

    That would create an error when the macro is actually used. Isn't that  what you wanted? I suppose I don't understand your use case.


    Igor Tandetnik

    I want to force a preprocessing error without causing any output from the macro other than the compiler saying an error has occured. It does seem your macro should do that.

    The amazing thing about your macro is that it almost worked. When the macro was invoked it did cause a preprocessing error message from the VC++ compiler, which is what I wanted, but when the macro was not invoked it caused the VC++ compiler to emit:

    fatal error C1009: compiler limit : macros nested too deeply

    which is definitely not what I want. Hard to believe but true, but then anything that the VC++ preprocessor does ( it breaks easily and does not fllow the C++ standard ) does not amaze me any longer.

    Saturday, July 9, 2011 10:01 PM
  • eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate  a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    Why do you think that will force a preprocessing error ? That just  creates output without forcing a preprocessing error.

    That would create an error when the macro is actually used. Isn't that  what you wanted? I suppose I don't understand your use case.


    Igor Tandetnik

    I need to explain my first reply better. The macro generating the preprocessing error, which you suggested, is being invoked by another macro. But that other macro is never being invoked during the logic of expanding a higher level macro in the source code of a .cpp file. In that case, nonetheless, I am getting an error from VC++ of:

    fatal error C1009: compiler limit : macros nested too deeply

    But when my source code in my .cpp does call the higher level macro with parameters which causes the lower level macro to be called which invokes the macro you suggested for causing the preprocessing error, a preprocessing error does get called, which is what I want, but not the one above.

    Strange but true.

    Saturday, July 9, 2011 11:00 PM
  • eldiener wrote:

    eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one  generate a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    Why do you think that will force a preprocessing error ? That just  creates output without forcing a preprocessing error.

    That would create an error when the macro is actually used. Isn't  that what you wanted? I suppose I don't understand your use
    case.

    I need to explain my first reply better. The macro generating the  preprocessing error, which you suggested, is being invoked by
    another macro. But that other macro is never being invoked during the  logic of expanding a higher level macro in the source code
    of a .cpp file. In that case, nonetheless, I am getting an error from  VC++ of: 

    fatal error C1009: compiler limit : macros nested too deeply

    But when my source code in my .cpp does call the higher level macro  with parameters which causes the lower level macro to be
    called which invokes the macro you suggested for causing the  preprocessing error, a preprocessing error does get called, which is
    what I want, but not the one above. 

    I have no idea what you are talking about. Can you show a complete  example that reproduces the problem?


    Igor Tandetnik

    Sunday, July 10, 2011 12:08 AM
  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    Similarly you might be able to utilize the static_assert concept, e.g.

    #define MY_MACRO() static_assert(0, "MY_MACRO caused error.")
    

    Monday, July 11, 2011 2:48 PM
  • eldiener wrote:

    eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one  generate a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    Why do you think that will force a preprocessing error ? That just  creates output without forcing a preprocessing error.

    That would create an error when the macro is actually used. Isn't  that what you wanted? I suppose I don't understand your use
    case.

    I need to explain my first reply better. The macro generating the  preprocessing error, which you suggested, is being invoked by
    another macro. But that other macro is never being invoked during the  logic of expanding a higher level macro in the source code
    of a .cpp file. In that case, nonetheless, I am getting an error from  VC++ of: 

    fatal error C1009: compiler limit : macros nested too deeply

    But when my source code in my .cpp does call the higher level macro  with parameters which causes the lower level macro to be
    called which invokes the macro you suggested for causing the  preprocessing error, a preprocessing error does get called, which is
    what I want, but not the one above. 

    I have no idea what you are talking about. Can you show a complete  example that reproduces the problem?


    Igor Tandetnik

    The actual example which causes the problem is very complicated ( usage with Boost PP ). I will attempt to reduce it if I can.
    Thursday, July 14, 2011 2:16 PM
  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    > anything that the VC++ preprocessor does ( it breaks easily and does not fllow the C++ standard ) does not amaze me any longer.

    Hmm... amazing anyone hardly was a goal for the VC++ preprocessor developers.

    --pa

    I need to generate a compile-time error, not a run-time error.
    Thursday, July 14, 2011 2:17 PM
  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    Similarly you might be able to utilize the static_assert concept, e.g.

     

    #define MY_MACRO() static_assert(0, "MY_MACRO caused error.")
    

     


    This is a good idea but I need a solution for older versions of VC++ which do not have C++0x support. Like static_assert my solution must produce a compile-time error.
    Thursday, July 14, 2011 2:19 PM
  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    Similarly you might be able to utilize the static_assert concept, e.g.

     

    #define MY_MACRO() static_assert(0, "MY_MACRO caused error.")
    

     


    This is a good idea but I need a solution for older versions of VC++ which do not have C++0x support. Like static_assert my solution must produce a compile-time error.

    I think Pavel A's suggestion of C_ASSERT is a compile time assertion check that should be available on some older versions of VC++.  If you aren't already using the Windows functionality where it is defined (winnt.h), you should be able to role your own version of the concept, e.g.:

    #define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]
    

    Kind of ugly, but maybe it will do until a better solution is found.

    • Marked as answer by Rob Pan Monday, July 18, 2011 9:18 AM
    Thursday, July 14, 2011 2:49 PM
  • Maybe C_ASSERT macro is what you want? (compile-time assertion)

    Similarly you might be able to utilize the static_assert concept, e.g.

    #define MY_MACRO() static_assert(0, "MY_MACRO caused error.")
    
    

     


    The static_assert is not for macroes, it works during compiling time, which is after the macro decoding proceed.
    - I like windows OSs and wanna work for them - We r the coders, U will be reprogrammed, resistance cause unhandled exceptions... - Recent code Lab: http://cppwindowsapi.codeplex.com/
    • Marked as answer by Rob Pan Monday, July 18, 2011 9:17 AM
    Thursday, July 14, 2011 5:43 PM
  • eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate a  preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message


    Igor Tandetnik

    What I need is a macro which forces a preprocessor error without outputting anything. The way of doing this which works with other compilers is to have the macro invoke another macro with the wrong number of macro parameters. But VC++ will not produce a preprocessor error but only a warning message.
    Thursday, July 21, 2011 12:34 PM
  • eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate  a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    What I need is a macro which forces a preprocessor error without  outputting anything.

    What do you mean by "preprocessor error"? I'm not familiar with the  term. How is it different from any other kind of error, and why does the  distinction matter?

    What's the use case? Let's assume you have the macro you want: can you  show an example of how you would use it, and what output from the  compiler you would expect?


    Igor Tandetnik

    Thursday, July 21, 2011 12:48 PM
  • eldiener wrote:

    eldiener wrote:

    Here is a C++ preprocessing challenge for VC++: how can one generate  a preprocessing error from within a C++ macro in VC++ ?

    #define MY_MACRO ~!%^&* Your error message

    What I need is a macro which forces a preprocessor error without  outputting anything.

    What do you mean by "preprocessor error"? I'm not familiar with the  term. How is it different from any other kind of error, and why does the  distinction matter?

    What's the use case? Let's assume you have the macro you want: can you  show an example of how you would use it, and what output from the  compiler you would expect?


    Igor Tandetnik

    I should have said "compiler error" instead of "preprocessor error". I want to force a compiler error when a macro is invoked without the macro itself producing any output which forces the error. For instance you suggested:

    #define MY_MACRO ~!%^&* Your error message

    but this causes a compiler error by producing invalid output. I do not want any output produced from the macro, but I want to force a compiler error.

    The use case is that a macro invoking MY_MACRO above may choose to ignore the output. I am sure you know how this can be done with macros. So I need MY_MACRO to force a compiler error without expanding to anything. In other words the expansion of MY_MACRO to produce the compiler is not what I need but rather something which MY_MACRO invokes. I gave the case that MY_MACRO can invoke some other macro with the wrong number of parameters, but unfortunately VC++ only gives a warning rather than an error. Nor can MY_MACRO itself output an #error directive, since this is not allowed in C++.

    If there is no way to do this with VC++, then there is no way. As I said other compilers I have tested will put out a compiler error if a macro invokes another macro with the wrong number of parameters. But no matter in VC++ whether I invoke another macro with too many or too little parameters, VC++ just gives a warning and continues on. Nor is there a way to change the warning to an error just for MY_MACRO.

     

     

     

     

    Thursday, July 21, 2011 1:09 PM