c:/temp/main.cpp(104): TODO - add code to implement this
__LINE__
マクロはint
に展開されているようで、書き込みができません#pragma message( __FILE__ "("__LINE__"): ..." )
// 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!
#error
を使用するという私の古いソリューションを確実に上回ります:D#define _STR(x) #x
#define STR(x) _STR(x)
#define TODO(x) __pragma(message("TODO: "_STR(x) " :: " __FILE__ "@" STR(__LINE__)))
//in code somewhere
TODO(Fix this);
1>TODO: Fix this :: c:\users\administrator\documents\visual studio 2008\projects\metatest\metatest\[email protected]
__pragma
を使用して(コンソールペインのメッセージをダブルクリックして)この行にジャンプできないことです(ただし、#pragma
でテストすると、とにかくそうではないようです...)
#define MacroStr(x) #x
#define MacroStr2(x) MacroStr(x)
#define Message(desc) __pragma(message(__FILE__ "(" MacroStr2(__LINE__) ") :" #desc))
Message("Need to add unit testing here")
#pragma
ディレクティブをパンチするのが面倒だと思う人のための答えの補足です。これを行うためにマクロをホイップすることで、いくつかのキーストロークを節約できます。一般に、マクロ内に#pragma
ディレクティブを含めることはできませんが、MS C / j_C++ _ jコンパイラ2008以降は、マクロで使用できる__pragma
と呼ばれる特別なベンダー固有の拡張機能をサポートしています。 Pragma Directives and the __Pragma Keywordを参照してください。#define STR2(x) #x
#define STR1(x) STR2(x)
#define LOC __FILE__ "("STR1(__LINE__)") : Warning Msg: "
#define WARNING_BUILDER(x) __FILE__ "("STR1(__LINE__)") : Warning Msg: " __FUNCTION__ " requires " #x
#define WREVIEW WARNING_BUILDER(review)
#define WUT WARNING_BUILDER(unit-testing)
#ifdef SPECIAL_WARNINGS
#ifdef SPECIAL_WARNINGS_REVIEW
#define MARK_FOR_REVIEW() do { \
__pragma(message( WREVIEW )) \
} while (0)
#else
#define MARK_FOR_REVIEW
#endif
#ifdef SPECIAL_WARNINGS_UNIT_TEST
#define MARK_FOR_UNIT_TEST() do { \
__pragma(message( WUT )) \
} while (0)
#else
#define MARK_FOR_UNIT_TEST
#endif
#endif
// uncomment/set in build-environment to enable special warnings
//#define SPECIAL_WARNINGS
#ifdef SPECIAL_WARNINGS
// uncomment/set in build-environment if you want only code review warnings
//#define SPECIAL_WARNINGS_REVIEW
// uncomment/set in build-environment if you want only unit-test warnings
//#define SPECIAL_WARNINGS_UNIT_TEST
#endif
int main()
{
MARK_FOR_REVIEW();
MARK_FOR_UNIT_TEST();
}
#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ")" ": warning: [blah]")
_CRT_STRINGIZE
は、多くの場合、すでにいくつかのヘッダーで定義されていますが、そうでない場合は、次のように定義できます。#define _CRT_STRINGIZE_(x) #x
#define _CRT_STRINGIZE(x) _CRT_STRINGIZE_(x)