预处理器中的sizeof [英] sizeof in preprocessor
问题描述
我怎样才能正确写出来?
#if sizeof(int)< sizeof(my_int)
..............
#endif
我想要为不同的平台编译不同的代码片段
"< - Chameleon - >" < CH ****** @ hotmail.NOSPAM.com>在消息中写道
news:c4 ********** @ nic.grnet.gr ...我该怎么写这个呢?
#if sizeof(int)< sizeof(my_int)
.............
#endif
我希望为不同的平台编译不同的代码片段
你不能在预处理器中,但你可以使用
基于sizeof
的模板专业化/ blockquote>
- Chameleon - 写道:
我怎样才能正确写出这个?
#if sizeof(int)< sizeof(my_int)
.............
#endif
你不能。预处理器不评估sizeof()的值。
因此,你不能在预处理器条件中使用它。你可以这样做:
#include< climits>
#define MY_INT_MAX some_value
#if INT_MAX< MY_INT_MAX
//东西
#else
//别的东西
#endif
或您使用模板:(未经测试)
模板< bool b>
void my_function_internal()
{
//东西
}
模板<>
void my_function_internal< ; false>()
{
//其他东西
}
void my_function( )
{
my_function_internal<(sizeof(int)< sizeof(my_int))>();
}
"< - Chameleon - >" < CH ****** @ hotmail.NOSPAM.com>在消息中写道
news:c4 ********** @ nic.grnet.gr ...我该怎么写这个呢?
#if sizeof(int)< sizeof(my_int)
.............
#endif
我希望为不同的平台编译不同的代码片段
你不能在预处理器中,但你可以使用
基于sizeof
的模板专业化/ BLOCKQUOTE>
How can I write this correctly?
#if sizeof(int) < sizeof(my_int)
..............
#endif
I want compile different pieces of code for different platforms解决方案
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c4**********@nic.grnet.gr...How can I write this correctly?
#if sizeof(int) < sizeof(my_int)
.............
#endif
I want compile different pieces of code for different platforms
You can''t in the preprocessor but you could use
specialization of templates based on sizeof
- Chameleon - wrote:
How can I write this correctly?
#if sizeof(int) < sizeof(my_int)
.............
#endif
You can''t. the value of sizeof() is not evaluated by the preprocessor.
Therefore, you can''t use it in preprocessor conditionals. You could do:
#include <climits>
#define MY_INT_MAX some_value
#if INT_MAX < MY_INT_MAX
//something
#else
//something else
#endif
or you use templates: (untested)
template <bool b>
void my_function_internal()
{
//something
}
template <>
void my_function_internal<false>()
{
//something else
}
void my_function()
{
my_function_internal<(sizeof(int) < sizeof(my_int))>();
}
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:c4**********@nic.grnet.gr...How can I write this correctly?
#if sizeof(int) < sizeof(my_int)
.............
#endif
I want compile different pieces of code for different platforms
You can''t in the preprocessor but you could use
specialization of templates based on sizeof
这篇关于预处理器中的sizeof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!