473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

sizeof in preprocessor

How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
..............
#endif

I want compile different pieces of code for different platforms
Jul 22 '05 #1
10 3995

"<- 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

Jul 22 '05 #2
- 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))>();
}

Jul 22 '05 #3

"<- 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

Jul 22 '05 #4
- 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))>();
}

Jul 22 '05 #5
<- Chameleon -> wrote:
How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
.............
#endif

I want compile different pieces of code for different platforms


I'd love to answer this for you, but since I'm Jewish, I assume you
don't want my help.

http://groups.google.com/groups?q=ch...rnet.gr&rnum=1
Jul 22 '05 #6
<- Chameleon -> wrote:
How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
.............
#endif

I want compile different pieces of code for different platforms


I'd love to answer this for you, but since I'm Jewish, I assume you
don't want my help.

http://groups.google.com/groups?q=ch...rnet.gr&rnum=1
Jul 22 '05 #7
> > How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
.............
#endif

I want compile different pieces of code for different platforms


I'd love to answer this for you, but since I'm Jewish, I assume you
don't want my help.


your answer is off-topic
Jul 22 '05 #8
> > How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
.............
#endif

I want compile different pieces of code for different platforms


I'd love to answer this for you, but since I'm Jewish, I assume you
don't want my help.


your answer is off-topic
Jul 22 '05 #9
<- Chameleon -> wrote:
How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
.............
#endif

I want compile different pieces of code for different platforms


I'd love to answer this for you, but since I'm Jewish, I assume you
don't want my help.

your answer is off-topic


Yes, that's what [OT] in the subject line means.
Jul 22 '05 #10
<- Chameleon -> wrote:
How can I write this correctly?

#if sizeof(int) < sizeof(my_int)
.............
#endif

I want compile different pieces of code for different platforms


I'd love to answer this for you, but since I'm Jewish, I assume you
don't want my help.

your answer is off-topic


Yes, that's what [OT] in the subject line means.
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: | last post by:
How can I write this correctly? #if sizeof(int) < sizeof(my_int) .............. #endif I want compile different pieces of code for different platforms
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
18
by: Kenneth Brody | last post by:
Is sizeof() legal within a #if preprocessor line? Specifically, I would like to use the following "sanity check" at compile time, but I'm getting an error on the compile about the #if line. ...
12
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
8
by: Mallesh | last post by:
Can anybody tell me how sizeof operator internally implemented. In my project i want to implemnent my own sizeof operator function (like mysizeof). How i can write the code? Please help me.
6
by: Amigo | last post by:
Hello all! I started working on an embedded project a few ago on Freescale 16-bit micro with an IAR toolset. Running PolySpace for the project code highlighted amongst other things a peculiar...
40
by: Boltar | last post by:
Hi Why - using gcc on linux - does this return 0 in C but returns 1 in C+ +? I don't get it. #include <stdio.h> struct foo { };
12
by: Ioannis Vranos | last post by:
In C99 it is mentioned: "The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.". If I am not wrong, this implies that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.