首页 > 开发 > C++ > 正文

这样写的用处是什么, 仅仅是为了区分不同系统的实现?

2017-09-11 21:34:53  来源: 网友分享

/** * Atomic add */template <typename ValueT>inline ValueT add_and_fetch(volatile ValueT *ptr, ValueT val);template <>inline uint64_t add_and_fetch(volatile uint64_t *ptr, uint64_t val){#if LEVELDB_IS_SOLARIS return atomic_add_64_nv(ptr, val);#else return __sync_add_and_fetch(ptr, val);#endif}template <>inline uint32_t add_and_fetch(volatile uint32_t *ptr, uint32_t val){#if LEVELDB_IS_SOLARIS return atomic_add_32_nv(ptr, val);#else return __sync_add_and_fetch(ptr, val);#endif}#if defined(__APPLE__) || defined(__OpenBSD__) || (defined(__s390__) && !defined(__s390x__))template <>inline size_t add_and_fetch(volatile size_t *ptr, size_t val){ return __sync_add_and_fetch(ptr, val);}#endif});

解决方案

The C standard does not exactly define sizes of short, int, long, long long and their unsigned versions. Only minimum sizes are enforced. For sake of example consider x86_64 architecture. long on Linux is 64-bit whereas on 64-bit Windows it is 32-bit. Common approach to make code more portable is to use length-specific types like uint16_t or int32_t defined by C99's stdint.h header file. Three kinds of integer types are defined there:

  • with exactly specified size: uint8_t ,uint16_t ,int32_t, etc.
  • smallest type with at least specified size: uint_least8_t, uint_least16_t, int_least32_t, etc.
  • most efficient type with at least specified size: uint_fast8_t, uint_fast16_t, int_fast32_t, etc.

一句话就是为了代码的可移值性。