为什么是wcout< “”;是OK,但wcout<<串();不是? [英] Why is it that wcout << ""; is OK but wcout << string(); is not?

查看:145
本文介绍了为什么是wcout< “”;是OK,但wcout<<串();不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #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 the basic_ostream<> class template with the argument wchar_t there will be one overload which will deal with narrow char 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 statement wcout << string("Hello"); does not compile because string does not have an implicit conversion to const char*, and because there is no overload of operator << that would insert a string 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 for 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
    );

As you can see, the same template parameter charT is used to instantiate both basic_ostream and basic_string.

这篇关于为什么是wcout&lt; “”;是OK,但wcout&lt;&lt;串();不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆