C++11 中没有"sto{short, unsigned short}"功能?
No "sto{short, unsigned short}" functions in C++11?
c++ 11引入了方便函数stoi
、stol
、stoll
、stoul
、stoull
、stof
、stod
、stold
,分别将字符串转换为整型、长型、长型、无符号长型、无符号长型、浮点型、双精度型和长双精度型。
为什么不喜欢short和unsigned short?
除了这个遗漏让我出于原则感到恼火之外,我发现自己不得不尴尬地处理这样的情况:
#include <string>
struct S
{
S(short);
};
int main()
{
S s{std::stoi("4")};
}
错误:test.cpp: In function 'int main()':
test.cpp:10:23: error: narrowing conversion from 'int' to 'short int' inside { } [-fpermissive]
我想写S s{std::stos("4")};
,如果只有stos
…
我不得不写S s{static_cast<short>(std::stoi("4"))};
…哦,等一下,这也不会做到这一点,它将默默地截断比短裤更长的整数,而不是假设的stos
函数,如果整数不适合短裤,则会抛出异常。所以基本上我回到了我在c++ 11之前的stringstreams
, boost::lexical_cast
等替代品。
编辑:由于人们似乎很难找到我的实际问题,它是为什么没有stos
和stous
函数与其他函数一起?
猜测:c++从C(可能是C99变体)中获取s-to-xxx函数只是为了兼容;如果c++是独立开发的,就不会有这样的函数了。
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 'short int'持有的值溢出,但"自动"不会溢出?
- 我应该删除矢量<short>吗?
- 无法从'unsigned short [9]'转换为'char []'
- 'unsigned short var'与'unsigned char var [2]' C++
- 错误:从 'short unsigned int*' 到"短无符号 int"的转换无效|(在 C++ 中)
- 对'icu_56::UnicodeString::UnicodeString(signed char, unsigned short const*, int)' 的未定义引用
- 访问违规读取位置 0xCCCCCCCC.using bool & unsigned short
- gcc 奇怪的转换警告(<B>从"int"转换为"A::count_type {aka short unsigned int}"可能会改变其值)
- unsigned short和signed short比较wierd行为
- 将cast char键入为Unsigned short
- 奇怪的c++运算符(运算符unsigned short())
- 是实现定义的unsigned short + int类型
- 将 unicode 字符串作为 std::vector<unsigned short>
- C++11 中没有"sto{short, unsigned short}"功能?
- 将unsigned short转换为char
- 将"unsigned char"数组转换为其"unsigned short"对应项的有效方法是什么?
- 将unsigned short mat转换为opencv中的vector
- 快速将quint16/unsigned short添加到QByteArray中