[英]How do I output a compile-time numeric constant during compilation in Visual C++?
Visual C++ has #pragma message
that outputs a string into compiler output . Visual C ++有#pragma message
,它将字符串输出到编译器输出中 。 Now I have a factory: 现在我有一家工厂:
template<class Type>
CComPtr<Type> CreateComObject()
{
CComPtr<Type> newObject( new CComObject<Type> );
//do some tuning to the object
return newObject;
}
and I want to output the size of class that is passed to new
(namely sizeof( CComObject<Type> )
into the compiler output. Looks like #pragma message
only accepts strings. 我想将传递给new
的类的大小(即sizeof( CComObject<Type> )
输出到编译器输出中。看起来#pragma message
只接受字符串。
How can I output a compile-time numeric constant? 如何输出编译时数字常量?
If I understood your question correctly, then I think you can do this: 如果我理解你的问题,那么我认为你可以这样做:
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;
}
The value of sizeof(CComObject<Type>)
will be printed as warning messages during compilation. sizeof(CComObject<Type>)
将在编译期间作为警告消息打印。
See this small demo : http://www.ideone.com/Diiqy 请参阅这个小型演示: http : //www.ideone.com/Diiqy
Look at these messages (from the above link): 查看这些消息(来自上面的链接):
prog.cpp: In member function 'overflow::operator char() [with unsigned int size = 4u ]': prog.cpp:在成员函数'overflow :: operator char()[with unsigned int size = 4u ]':
prog.cpp: In member function 'overflow::operator char() [with unsigned int size = 12u ]': prog.cpp:在成员函数'overflow :: operator char()[with unsigned int size = 12u ]':
prog.cpp: In member function 'overflow::operator char() [with unsigned int size = 400u ]': prog.cpp:在成员函数'overflow :: operator char()[with unsigned int size = 400u ]'中:
In Visual Studio, you can see these messages in the Build Output tab; 在Visual Studio中,您可以在“ 构建输出”选项卡中看到这些消息; it may not appear in Error List > Warnings tab. 它可能不会出现在错误列表>警告选项卡中。
The idea is taken from my another solution: 这个想法取自我的另一个解决方案:
Calculating and printing factorial at compile time in C++ 在C ++中编译和打印在编译时的阶乘
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.