#ifdef for 32-bit platform

后端 未结 11 1532
眼角桃花
眼角桃花 2020-12-30 00:37

In an application I maintain, we\'ve encountered a problem with file descriptor limitations affecting the stdlib. This problem only affects the 32-bit version of the standar

11条回答
  •  余生分开走
    2020-12-30 00:59

    You could check a well known type for it's size e.g. sizeof(int*) == 4 for a 32 bit platform.

    As sizeof is known at compiletime I believe a

    if(sizeof(int*) == 4)
    {
      ...
    }
    

    should do the trick

    Edit: the comments are right, you need to use a regular if, #if won't work.

    If you are using C++ You could create templated code and let the compiler choose the specialization for you based on the sizeof() call. If you build for a 32 bit platform the compiler would only instantiate the code for the 32 bit platform. If you build for a 654 bit platform the compiler would only instantiate the code for the 64 bit platform.

提交回复
热议问题