为什么是wcout< “”;是OK,但wcout<<串();不是? [英] Why is it that wcout << ""; is OK but wcout << string(); is not?
问题描述
#include< iostream>
#include< string>
using namespace std;
int main()
{
wcout<< LHello; // 好。
wcout<< wstring(LHello); // 好。
wcout<< 你好; // 好。为什么?
wcout<< string(Hello); //错误。为什么?
}
为什么 std :: wcout
接受一个窄字符串文字作为其参数,但不接受一个窄字符串对象?
C ++ 11标准的第27.7.3.6.4节,其中指定了以下两个重载运算符(除其他外):
template< class charT,class traits>
basic_ostream< charT,traits>& operator<<<(
basic_ostream< charT,traits>& out,
const charT * s
);
template< class charT,class traits>
basic_ostream< charT,traits>& operator<<<(
basic_ostream< charT,traits>& out,
const char * s
);
最后一个重载明确处理 char
基于C字符串。这意味着即使对于具有参数 wchar_t
的 basic_ostream<>
类模板的实例化,将存在一个重载将处理窄的 char
字符串。
此外,根据§27.7.3.6.4 / 5:
填充按第22.4.2.2.2节所述确定。 使用out.widen(27.5.5.3)扩展从s开始的n个字符。扩展的字符和任何所需的填充插入到中。
wcout <$> <$> <$> <$> <$> <$> <$>
<$> <<因为string
没有隐式转换为const char *
code>,并且因为没有重载operator
,它将插入一个 string
将一个字符类型转换为具有不同底层字符类型的输出流。
在标准术语中(见第21.4.8.9节),下面是如何定义重载
运算符< <$ / code>看起来像
std :: string
:template< class charT,class traits,class Allocator>
basic_ostream< charT,traits>& operator<<<<(
basic_ostream< charT,traits>& os,
const basic_string< charT,traits,Allocator>& str
);
正如你所看到的,同样的模板参数
charT
用于实例化basic_ostream
和basic_string
。#include <iostream> #include <string> using namespace std; int main() { wcout << L"Hello"; // OK. wcout << wstring(L"Hello"); // OK. wcout << "Hello"; // OK. Why? wcout << string("Hello"); // Error. Why? }
Why does
std::wcout
accept a narrow string literal as its argument but doesn't accept a narrow string object?解决方案This is dictated by § 27.7.3.6.4 of the C++11 Standard, where the following two overloaded operators (among others) are specified:
template<class charT, class traits> basic_ostream<charT,traits>& operator<<( basic_ostream<charT,traits>& out, const charT* s ); template<class charT, class traits> basic_ostream<charT,traits>& operator<<( basic_ostream<charT,traits>& out, const char* s );
The last overload deals explicitly with
char
-based C-strings. This means that even for instantiations of thebasic_ostream<>
class template with the argumentwchar_t
there will be one overload which will deal with narrowchar
strings.Moreover, per § 27.7.3.6.4/5:
Padding is determined as described in 22.4.2.2.2. The n characters starting at s are widened using out.widen (27.5.5.3). The widened characters and any required padding are inserted into out. Calls width(0).
On the other hand, the statementwcout << string("Hello");
does not compile becausestring
does not have an implicit conversion toconst char*
, and because there is no overload ofoperator <<
that would insert astring
built with one character type into an output stream with a different underlying character type.In Standard terms (see § 21.4.8.9), here is how the definition of the overloaded
operator <<
looks like forstd::string
:template<class charT, class traits, class Allocator> basic_ostream<charT, traits>& operator<<( basic_ostream<charT, traits>& os, const basic_string<charT,traits,Allocator>& str );
As you can see, the same template parameter
charT
is used to instantiate bothbasic_ostream
andbasic_string
.这篇关于为什么是wcout< “”;是OK,但wcout<<串();不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!