How can I detect if I'm compiling for a 64bits architecture in C++

前端 未结 8 1666
难免孤独
难免孤独 2020-12-29 23:07

In a C++ function I need the compiler to choose a different block if it is compiling for a 64 bit architecture.

I know a way to do it for MSVC++ and g++, so I\'ll po

8条回答
  •  隐瞒了意图╮
    2020-12-29 23:15

    An architecture-independent way to detect 32-bit and 64-bit builds in C and C++ looks like this:

    // C
    #include 
    
    // C++
    #include 
    
    #if INTPTR_MAX == INT64_MAX
    // 64-bit
    #elif INTPTR_MAX == INT32_MAX
    // 32-bit
    #else
    #error Unknown pointer size or missing size macros!
    #endif
    

提交回复
热议问题