sizeof in preprocessor

G

Guest

How can I write this correctly?

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

I want compile different pieces of code for different platforms
 
N

Nick Hounsome

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
 
R

Rolf Magnus

- Chameleon - said:
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))>();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using sizeof in the preprocessor 7
Preprocessor problem 1
Preprocessor 4
Preprocessor 0
preprocessor idea 10
Preprocessor 4
Linux: using "clone3" and "waitid" 0
preprocessor 2

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top