c++ - 这样写的用处是什么, 仅仅是为了区分不同系统的实现?
高洛峰
高洛峰 2017-04-17 11:26:00
[C++讨论组]

/** * 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 });
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
高洛峰

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.

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号