Namespaces
Variants

std::unique_ptr<T,Deleter>:: operator<<

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
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

注释

如果 std:: unique_ptr < Y, D > :: pointer 是指向字符类型的指针(例如当 Y char ( [ ] ) 或 CharT ( [ ] ) 时),这可能会调用 针对空终止字符串的 operator<< 重载 (若该指针实际并未指向此类字符串将导致未定义行为),而非 用于输出指针本身值的重载

示例

#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

参见

返回指向被管理对象的指针
(公开成员函数)