C++ 为什么';操作员<<';存在于';char';std::basic_ostream的部分专门化?
我想知道为什么需要使用下面的C++ 为什么';操作员<<';存在于';char';std::basic_ostream的部分专门化?,c++,integer,char,operator-overloading,ostream,C++,Integer,Char,Operator Overloading,Ostream,我想知道为什么需要使用下面的操作符重载来允许在不指定所有模板类型的情况下使用操作符。如果类型匹配,编译器只使用正确的重载。如果它们不匹配,则必须进行隐式转换(不需要),或者必须显式指定所有模板类型,我甚至不确定是否有语法允许这样做。字符特性和各种字符重载无关 char特性与char类型和输出流的处理有关,而char重载则与插入的char类型有关。想想basic\u ostreamvs.operator@LokiAstari它与其中一个具有相同的表示形式,但它是一种不同的类型。IOStreams又
操作符重载来允许在不指定所有模板类型的情况下使用操作符。如果类型匹配,编译器只使用正确的重载。如果它们不匹配,则必须进行隐式转换(不需要),或者必须显式指定所有模板类型,我甚至不确定是否有语法允许这样做。字符特性和各种字符重载无关
char特性与char类型和输出流的处理有关,而char重载则与插入的char类型有关。想想basic\u ostream
vs.operator@LokiAstari它与其中一个具有相同的表示形式,但它是一种不同的类型。IOStreams又如何呢?我认为真正的问题不在于重载的存在,而在于它们的行为就像传递字符数据,而不是整数。这不是问题吗?@BenjaminLindley,是的,这是行为问题。:-)如果这些特定的重载不存在,我相信basic_ostream
的行为会更接近我的预期,并将signed char
和unsigned char
视为整数类型。但在创建库时,添加特定重载函数肯定是有原因的。在我的解决方案示例中,我们已经必须显式地将单个值强制转换为带符号的short
或无符号的short
,并指向void*
;这些参数类型作为成员函数重载显式提供。我猜备选方案必须包括有符号字符
和无符号字符
的成员函数重载,用于所有基本流
的成员函数重载。我知道基本流
的第二个模板类型用于格式化基本流
本身,不使用运算符我的观点是有符号字符
和无符号字符
类型应被视为任何其他整数(和非字符)类型。目前,signed char integer=33;标准::cout
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
char ch );
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
signed char ch );
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
unsigned char ch );
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
const char* s );
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
const signed char* s );
template< class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,traits>& os,
const unsigned char* s );
template< class CharT, class Traits>
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
CharT ch );
template< class CharT, class Traits>
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
char ch );
template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
const CharT* s );
template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
const char* s );