unique_ptr_operator_ltlt

unique_ptr_operator_ltlt (unique_ptr 출력 연산자)

<memory> 헤더에 정의되어 있어요. p가 관리하는 포인터의 값을 출력 스트림 os에 삽입해요. os << p.get()과 동등해요. C++20부터 도입됐어요.

출처: cppreference

본문

template< class CharT, class Traits, class Y, class D >
std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os,
                                               const std::unique_ptr<Y, D>& p );

(C++20부터)

p가 관리하는 포인터의 값을 출력 스트림 os에 삽입해요. os << p.get()과 동등해요. 이 오버로드는 os << p.get()이 유효한 표현식일 때만 오버로드 해석에 참여해요.

매개변수

  • os: p를 삽입할 std::basic_ostream
  • p: os에 삽입할 포인터

반환값

os.

주의 (Notes)

std::unique_ptr<Y, D>::pointer가 문자 타입에 대한 포인터일 때(예: Ychar[] 또는 CharT[]인 경우), 포인터 자체의 값을 출력하는 오버로드 대신 널 종료 문자열 오버로드가 호출될 수 있어요(그 포인터가 실제로 그런 문자열을 가리키지 않으면 미정의 동작이 될 수 있어요).

예제

#include <iostream>
#include <memory>

class Foo {};

int main()
{
    auto p = std::make_unique<Foo>();
    std::cout << p << '\n';
    std::cout << p.get() << '\n';
}

가능한 출력:

0x6d9028
0x6d9028

일반 출력 스트림에 unique_ptr을 직접 출력하면 관리하는 포인터의 주소가 16진수로 나와요.

더 알아보기 (Learn more)

cppreference