为什么有时人们更喜欢Strtoll而不是Strtol,即使他们只想将字符串转换为INT32
Why sometimes people prefer strtoll to strtol even they only want to convert a string to an int32?
这是我从Google的Gflags源代码
中读取的代码段case FV_INT32: {
const int64 r = strto64(value, &end, base);
if (errno || end != value + strlen(value)) return false; // bad parse
if (static_cast<int32>(r) != r) // worked, but number out of range
return false;
SET_VALUE_AS(int32, static_cast<int32>(r));
return true;
}
和宏定义strto64
// Work properly if either strtoll or strtoq is on this system
#ifdef HAVE_STRTOLL
# define strto64 strtoll
# define strtou64 strtoull
#elif HAVE_STRTOQ
# define strto64 strtoq
# define strtou64 strtouq
#else
// Neither strtoll nor strtoq are defined. I hope strtol works!
# define strto64 strtol
# define strtou64 strtoul
#endif
显然,作者更喜欢Strtoll而不是Strtol。根据这两个功能的人页面, 一个返回 long long int ,另一个返回 long int 。如果您只想要INT32,他们都可以,对吗?
那么,这两个功能有什么区别?为什么首选Strtoll?
您的问题实际上是:"当标志为 FV_INT32
时,在此处使用strto64
?":
由于此检查,此代码似乎更喜欢strto64
:
if (static_cast<int32>(r) != r)
因此,它首先尝试尽可能多地吞噬数字,因此strto64
。有了可以舒适地检查适合32位的
关于strtoll
:long long
保证至少64位宽。因此,对于strto64
。
在支持long long int
的系统上,它至少是64位整数。另一方面,long int
的保证是至少是一个32位整数。在某些系统/使用某些编译器上,long int
是一个64位整数。在其他方面,只有32位。Google标头希望long int
在不提供strtoll
或strtoq
的系统上确实是64位,因为否则无法将字符串转换为long long int
。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 我只想在Android 4.4中使用C++11库
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 我怎样才能有 2 个程序并让用户选择他们想要使用的程序?
- 我只想获得高质量的功能点
- cuda 和 opencv:我只想要与原始图片相同的图片
- 我想在C++中安排结构.通过他们的标记以及他们的其他属性
- 想知道为什么我不能只捕获协程的 asio::handler_type 的引用
- 我只想从文本文件中删除一个特定值或行
- 为什么有时人们更喜欢Strtoll而不是Strtol,即使他们只想将字符串转换为INT32
- 当我只想禁用自定义赋值运算符=()的签名时,它是否重要
- 我试图证明一个给定的树是一个二进制搜索树.我将输入一个二叉树,我只想让函数返回true
- 如果我只想要架构良好的工作区,但不一定是分开的编译,如何在C++中组织头/代码文件
- Qt GUI-只想在值更改时读取.txt文件
- 当一个进程崩溃时,我怎么能压制所有的错误对话框(我只想让它安静地崩溃)
- 如果我只想在模板中专门研究一种方法,我该怎么做
- 访问一个我只想存在于派生类中的函数
- 哪一个更快,为什么?1.阵列2.链接列表.如果我们只想在for循环中迭代并打印它
- 如果我只想读取数字,我如何忽略文本文件中的单词?
- 如果我只想指定哈希函数,我应该传递给unordered_map的存储桶计数参数什么?