为什么C++ to_string函数不支持char和unsigned char类型?

zoh*_*har 3 c++

在cppreference.com上,它说该to_string函数可以通过以下方式工作。为什么没有char输入unsigned char

to_string(int value);
to_string(long value);
to_string(long long value);
to_string(unsigned value);
to_string(unsigned long value);
to_string(unsigned long long value);
to_string(float value);
to_string(double value);
to_string(long double value);
Run Code Online (Sandbox Code Playgroud)

use*_*570 6

为什么没有 char 和 unsigned char 类型?

因为已经有一个std::string可以std::string::string(size_type, char)接受char. 特别是,来自basic_string

//-----------------------------vvvvv------------------->character type parameter
basic_string( size_type count, CharT ch,
              const Allocator& alloc = Allocator() );
Run Code Online (Sandbox Code Playgroud)

这意味着to_string我们可以不使用 ,而只使用

std::string str(1, 'c');
Run Code Online (Sandbox Code Playgroud)

将使用上面的构造函数。

基本上,我们不需要重载to_stringfor,char因为std::string已经有可用的构造函数。