0

I have a basic #pragma message warning

#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ") : warning : T does not have an << operator.")

This is inside a Sfinae controlled overload testing for the presence of a << operator. This warning works and gets printed to the output window and added to the Error List in VS2019.

However it is missing the extra info that "native" warnings and errors give:

[ with T = int ]

And the extra stack/instantiation trace, allowing you to work out exactly which function call is causing the issue.

Is there a way to have my warning also display this extra useful info, as it stands my warning is unable to even tell the user what type triggered the warning, let alone which section of code/method call is causing the warning.

__PRETTY_FUNCTION__ for example does not work in #pragma message as it is a const char[] and #pragma message requires a constant string i.e. "bla bla".

0

2 Answers 2

0

A bit unorthodox, but you could use a [[deprecated("...")]] in front of your SFINAE-controlled overload to have info about both the actual type and the call stack.

1
  • I have seen the use of deprecated warnings and it may be a last resort, however it is not a clear warning, even when paired with my own warning it may confuse users of the class.
    – uhsl_m
    Commented Mar 24, 2021 at 16:32
0

I suggest that you could refer to this link.

 // Statements like:
 // #pragma message(Reminder "Fix this problem!")
 // Which will cause messages like:
 // C:\Source\Project\main.cpp(47): Reminder: Fix this problem!
 // to show up during compiles. Note that you can NOT use the
 // words "error" or "warning" in your reminders, since it will
 // make the IDE think it should abort execution. You can double
 // click on these messages and jump to the line in question.

 #define Stringize( L )     #L 
 #define MakeString( M, L ) M(L)
 #define $Line MakeString( Stringize, __LINE__ )
 #define Reminder __FILE__ "(" $Line ") : Reminder: "

Once defined, use like so:

#pragma message(Reminder "Fix this problem!") 

This will create output like:

C:\Source\Project\main.cpp(47): Reminder: Fix this problem!

Besides, while in general, you cannot have a #pragma directive within macros, MS C/C++ compilers 2008 and above do support a special vendor-specific extension called the __pragma which can be used with macros.

2
  • My message can already do all that. Line number is not an issue, the issue is that this is a templated method deep in a utility class. Giving this line number is not much use to a developer using this class. It would be more useful to them to see the same instantiation stack you get in the output view of VS when inbuilt errors or warnings occur along with the [ with T = double ] information.
    – uhsl_m
    Commented Mar 25, 2021 at 10:20
  • According to your description, it seems that this method deprecated is only available. Commented Mar 26, 2021 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.