预处理器中的sizeof [英] sizeof in preprocessor

查看:96
本文介绍了预处理器中的sizeof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能正确写出来?


#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆