Namespaces
Variants

operator<< (std::basic_ostream)

From cppreference.net
**변역 설명:** - `operator` → `연산자` (C++ 연산자 키워드 번역) - `delete` → `삭제됨` (C++ delete 키워드 번역) - HTML 태그, 속성, ` ` 내의 C++ 코드는 모두 원본 유지 - C++ 특수 용어(basic_ostream, wchar_t, char16_t, template, class, const 등)는 번역하지 않음 - 코드 구조와 서식 완전히 보존 HTML 태그, 속성, 코드 블록 내의 텍스트는 번역하지 않고 그대로 유지되었습니다. C++ 관련 용어(template, class, basic_ostream, wchar_t, operator, const, char32_t, delete 등)도 원문 그대로 보존되었습니다. (원문에 번역할 텍스트가 없으므로 동일한 HTML 구조를 유지합니다)
헤더 파일에 정의됨 <ostream>
basic_ostream과 문자
(1)
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 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 ) ;
basic_ostream과 문자 배열
(2)
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 ) ;
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 ) ;
basic_ostream rvalue
template < class Ostream, class T >
Ostream && operator << ( Ostream && os, const T & value ) ;
(3) (C++11 이후)
basic_ostream 및 UTF 문자/배열에 대한 삭제된 오버로드
(4) (C++20 이후)
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, wchar_t ch ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, char8_t ch ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, char16_t ch ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, char32_t ch ) = delete ;
template < class Traits >

basic_ostream < wchar_t , Traits > &

연산자 << ( basic_ostream < wchar_t , Traits > & os, char8_t ch ) = 삭제됨 ;
template < class Traits >

basic_ostream < wchar_t , Traits > &

operator << ( basic_ostream < wchar_t , Traits > & os, char16_t ch ) = delete ;
template < class Traits >

basic_ostream < wchar_t , Traits > &

operator << ( basic_ostream < wchar_t , Traits > & os, char32_t ch ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, const wchar_t * s ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, const char8_t * s ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, const char16_t * s ) = delete ;
template < class Traits >

basic_ostream < char , Traits > &

operator << ( basic_ostream < char , Traits > & os, const char32_t * s ) = delete ;
template < class Traits >

basic_ostream < wchar_t , Traits > &

operator << ( basic_ostream < wchar_t , Traits > & os, const char8_t * s ) = delete ;
template < class Traits >

basic_ostream < wchar_t , Traits > &

연산자 << ( basic_ostream < wchar_t , Traits > & os, const char16_t * s ) = 삭제됨 ;
template < class Traits >

basic_ostream < wchar_t , Traits > &

operator << ( basic_ostream < wchar_t , Traits > & os, const char32_t * s ) = delete ;

문자 또는 문자열을 삽입합니다.

1) FormattedOutputFunction 로 동작합니다. sentry 객체를 생성하고 검사한 후, 문자 ch 를 삽입합니다. ch char 타입이고 os character container type char 가 아닌 경우, 대신 os. widen ( ch ) 가 삽입됩니다.
패딩은 다음과 같이 결정됩니다:
  • 만약 os. width ( ) > 1 이면, os. width ( ) - 1 개의 os. fill ( ) 복사본이 출력 문자에 추가되어 출력 문자 시퀀스를 형성합니다.
  • 만약 ( out. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left 이면, 채움 문자가 출력 문자 뒤에 배치되고, 그렇지 않으면 앞에 배치됩니다.
삽입 후, os. width ( 0 ) 가 호출되어 std::setw 의 효과가 있을 경우 이를 취소합니다.
2) FormattedOutputFunction 으로 동작합니다. sentry 객체를 구성하고 검사한 후, s 가 가리키는 첫 번째 요소를 갖는 문자 배열에서 연속적인 문자들을 삽입합니다.
  • 첫 번째와 세 번째 오버로드( CharT ch 의 타입과 일치하는 경우)에서는 정확히 traits :: length ( s ) 개의 문자가 삽입됩니다.
  • 두 번째 오버로드에서는 정확히 std:: char_traits < char > :: length ( s ) 개의 문자가 삽입됩니다.
  • 마지막 두 오버로드에서는 정확히 traits :: length ( reinterpret_cast < const char * > ( s ) ) 개의 문자가 삽입됩니다.
삽입 전에 먼저 모든 문자는 os. widen ( ) 를 사용하여 확장된 후, 패딩이 다음과 같이 결정됩니다:
  • 삽입할 문자 수가 os. width ( ) 보다 적은 경우, os. fill ( ) 의 복사본을 충분히 추가하여 문자 시퀀스의 길이를 os. width ( ) 와 동일하게 만듭니다.
  • 만약 ( out. flags ( ) & std:: ios_base :: adjustfield ) == std:: ios_base :: left 인 경우, 채움 문자는 출력 시퀀스의 끝에 추가됩니다. 그렇지 않으면 출력 시퀀스 앞에 추가됩니다.
삽입 후, os. width ( 0 ) 가 호출되어 std::setw 의 효과가 있을 경우 이를 취소합니다.
만약 s 가 널 포인터인 경우, 동작은 정의되지 않습니다.
3) 출력 스트림 객체에 대한 rvalue 참조가 주어졌을 때 적절한 삽입 연산자를 호출합니다( os << value 와 동등함). 이 오버로드는 os << value 표현식이 well-formed이고 Ostream std::ios_base 로부터 공개적이고 명확하게 파생된 클래스 타입인 경우에만 오버로드 해결에 참여합니다.
4) char16_t , char32_t 등을 허용하는 오버로드(또는 이들의 null 종료 시퀀스)는 삭제되었습니다: std:: cout << u 'X' 는 허용되지 않습니다. 이전에는 이러한 코드가 정수나 포인터 값을 출력했습니다.

목차

매개변수

os - 데이터를 삽입할 출력 스트림
ch - 삽입할 문자에 대한 참조
s - 삽입할 문자 문자열에 대한 포인터

반환값

1,2) os
3) std :: move ( os )

참고 사항

이전에 LWG 이슈 1203 이전에는, ( std:: ostringstream ( ) << 1.2 ) . str ( ) 와 같은 코드는 컴파일되지 않았습니다.

예제

#include <fstream>
#include <iostream>
void foo()
{
    // error: operator<< (basic_ostream<char, _Traits>&, char8_t) is deleted
//  std::cout << u8'z' << '\n';
}
std::ostream& operator<<(std::ostream& os, char8_t const& ch)
{
    return os << static_cast<char>(ch);
}
int main()
{
    std::cout << "Hello, world" // uses `const char*` overload
              << '\n';          // uses `char` overload
    std::ofstream{"test.txt"} << 1.2; // uses rvalue overload
    std::cout << u8'!' << '\n'; // uses program-defined operator<<(os, char8_t const&)
}

출력:

Hello, world
!

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 167 C++98 (2) 에 있는 모든 오버로드에 대해 삽입된
문자 수가 traits :: length ( s ) 였음
CharT ch 의 타입과 일치하지 않는
오버로드들의 숫자를 업데이트함
LWG 1203 C++11 rvalue 스트림에 대한 오버로드가
기본 클래스의 lvalue 참조를 반환함
파생 클래스의 rvalue 참조를
반환함
LWG 2011 C++98 패딩이 std::num_put::do_put() 에 의해 결정됨 연산자 자체에 의해 결정됨
LWG 2534 C++11 rvalue 스트림에 대한 오버로드가 제약되지 않았음 제약됨

참고 항목

형식화된 데이터 삽입
(public member function)
인수의 형식화된 표현 출력
(function template)
문자 확장
(public member function of std::basic_ios<CharT,Traits> )