Cross platform version of __PTRDIFF_TYPE__

335 Views Asked by At

Is there a cross-platform way to determine whether ptrdiff_t is the same as long or long long during preprocessing?

I know __PTRDIFF_TYPE__ should give the type with gcc/g++, but it doesn't seem to be defined in VC/VC++ for Windows. Is there a better approach than the following?

#ifndef __PTRDIFF_TYPE__
# if _WIN64
#  define __PTRDIFF_TYPE__ long long
# else
#  define __PTRDIFF_TYPE__ long
# endif
#endif

If not possible during preprocessing, is there a compile time approach? I'm looking for a non-C++11 solution, but if you've got a really nice modern solution, feel free to share!

1

There are 1 best solutions below

3
On BEST ANSWER

A compile-time approach is very obvious:

if (sizeof(ptrdiff_t) == sizeof(long))

or

if (sizeof(ptrdiff_t) == sizeof(long long))

TMK, there are no portable defines for this. However, this is just a minor obstacle. With just a little bit of scripting, any compile-time test of this nature can be trivially converted to, essentially, a preprocessor-based test, using standard tools like autoconf and automake. These are standard tools used by thousands of free software libraries and tools, for this precise purpose.