[英]How do I output a compile-time numeric constant during compilation in Visual C++?
Visual C ++有#pragma message
,它將字符串輸出到編譯器輸出中 。 現在我有一家工廠:
template<class Type>
CComPtr<Type> CreateComObject()
{
CComPtr<Type> newObject( new CComObject<Type> );
//do some tuning to the object
return newObject;
}
我想將傳遞給new
的類的大小(即sizeof( CComObject<Type> )
輸出到編譯器輸出中。看起來#pragma message
只接受字符串。
如何輸出編譯時數字常量?
如果我理解你的問題,那么我認為你可以這樣做:
template<size_t size>
struct overflow{ operator char() { return size + 256; } }; //always overflow
//if you doubt, you can use UCHAR_MAX +1 instead of 256, to ensure overflow.
template<class Type>
CComPtr<Type> CreateComObject()
{
CComPtr<Type> newObject( new CComObject<Type> );
char(overflow<sizeof(CComObject<Type>)>());
return newObject;
}
sizeof(CComObject<Type>)
將在編譯期間作為警告消息打印。
請參閱這個小型演示: http : //www.ideone.com/Diiqy
查看這些消息(來自上面的鏈接):
prog.cpp:在成員函數'overflow :: operator char()[with unsigned int size = 4u ]':
prog.cpp:在成員函數'overflow :: operator char()[with unsigned int size = 12u ]':
prog.cpp:在成員函數'overflow :: operator char()[with unsigned int size = 400u ]'中:
在Visual Studio中,您可以在“ 構建輸出”選項卡中看到這些消息; 它可能不會出現在錯誤列表>警告選項卡中。
這個想法取自我的另一個解決方案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.