如何解决自定义#pragma 消息警告将实例化跟踪
我有一个基本的#pragma 消息警告
#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ") : warning : T does not have an << operator.")
这是在 Sfinae 控制的过载测试中是否存在
但是,它缺少“本机”警告和错误提供的额外信息:
[ with T = int ]
以及额外的堆栈/实例化跟踪,让您能够准确地找出导致问题的函数调用。
有没有办法让我的警告也显示这个额外的有用信息,就目前而言,我的警告甚至无法告诉用户是什么类型触发了警告,更不用说是哪部分代码/方法调用导致了警告。
__PRETTY_FUNCTION__
例如在 #pragma 消息中不起作用,因为它是一个 const char[] 并且 #pragma 消息需要一个常量字符串,即“bla bla”。
解决方法
有点不正统,但您可以在 SFINAE 控制的重载前使用 [[deprecated("...")]]
以获取有关实际类型和调用堆栈的信息。
我建议你可以参考这个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: "
一旦定义,像这样使用:
#pragma message(Reminder "Fix this problem!")
这将创建如下输出:
C:\Source\Project\main.cpp(47): Reminder: Fix this problem!
此外,虽然一般来说,宏中不能有 #pragma
指令,但 MS C/C++ 编译器 2008 及更高版本确实支持一个特殊的供应商特定扩展,称为 __pragma
,可与宏。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。