uhsl_m
uhsl_m

Reputation: 342

Custom #pragma message warning will instantiation trace

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".

Upvotes: 0

Views: 557

Answers (2)

Barrnet Chou
Barrnet Chou

Reputation: 1933

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.

Upvotes: 0

m88
m88

Reputation: 1988

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.

Upvotes: 0

Related Questions