Namespaces
Variants

std::optional<T>:: optional

From cppreference.net
Utilities library
constexpr optional ( ) noexcept ;
(1) (C++17부터)
constexpr optional ( std:: nullopt_t ) noexcept ;
(2) (C++17부터)
constexpr optional ( const optional & other ) ;
(3) (C++17부터)
constexpr optional ( optional && other ) noexcept ( /* 아래 참조 */ ) ;
(4) (C++17부터)
template < class U >
optional ( const optional < U > & other ) ;
(5) (C++17부터)
(C++20부터 constexpr)
(조건부 explicit)
template < class U >
optional ( optional < U > && other ) ;
(6) (C++17부터)
(C++20부터 constexpr)
(조건부 explicit)
template < class ... Args >
constexpr explicit optional ( std:: in_place_t , Args && ... args ) ;
(7) (C++17부터)
template < class U, class ... Args >

constexpr explicit optional ( std:: in_place_t ,
std:: initializer_list < U > ilist,

Args && ... args ) ;
(8) (C++17부터)
template < class U = std:: remove_cv_t < T > >
constexpr optional ( U && value ) ;
(9) (C++17부터)
(조건부 explicit)

새로운 optional 객체를 생성합니다.

목차

매개변수

other - 다른 optional 객체로부터 포함된 값을 복사
value - 포함된 값을 초기화하는 데 사용할 값
args... - 포함된 값을 초기화하는 데 사용할 인수들
ilist - 포함된 값을 초기화하는 데 사용할 초기화자 리스트

효과

오버로드 초기화 방식 포함된 값의 초기화자 has_value() 생성 후 상태
( 1 ) 해당 없음 - false
( 2 )
( 3 ) 직접 초기화 (비목록) * other other. has_value ( )
  • 만약 false 인 경우, 포함된 값은 초기화되지 않음
( 4 ) std :: move ( * other )
( 5 ) * other
( 6 ) std :: move ( * other )
( 7 ) std:: forward < Args > ( args ) ... true
( 8 ) ilist, std:: forward < Args > ( args ) ...
( 9 ) std:: forward < U > ( value )

제약 조건 및 보충 정보

3) 만약 std:: is_copy_constructible_v < T > false 인 경우, 생성자는 삭제된 것으로 정의됩니다.
만약 std:: is_trivially_copy_constructible_v < T > true 인 경우, 해당 생성자는 trivial합니다.
4) 이 오버로드는 std:: is_move_constructible_v < T > true 인 경우에만 오버로드 해결에 참여합니다.
만약 std:: is_trivially_move_constructible_v < T > true 라면, 해당 생성자는 trivial합니다.
5) 이 오버로드는 다음의 모든 조건이 만족될 때에만 오버로드 해결에 참여합니다:
이 오버로드는 다음과 같이 선언된 것처럼 동작합니다 explicit ( ! std:: is_convertible_v < const U & , T > ) .
6) 이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:
이 오버로드는 다음과 같이 선언된 것처럼 동작합니다 explicit ( ! std:: is_convertible_v < U, T > ) .
7) 이 오버로드는 다음 조건이 std:: is_constructible_v < T, Args... > true 일 때만 오버로드 해결에 참여합니다.
만약 T 의 초기화에 선택된 생성자가 constexpr 생성자라면, 이 생성자 또한 constexpr 생성자입니다.
8) 이 오버로드는 다음 조건이 std:: is_constructible_v < T, std:: initializer_list < U > & , Args... > 일 때만 오버로드 해결에 참여합니다.
만약 T 의 초기화에 선택된 생성자가 constexpr 생성자라면, 이 생성자 또한 constexpr 생성자입니다.
9) 이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:
이 오버로드는 다음과 같이 선언된 것처럼 동작합니다 explicit ( ! std:: is_convertible_v < U, T > ) .
만약 T 의 초기화에 선택된 생성자가 constexpr 생성자라면, 이 생성자 또한 constexpr 생성자입니다.
  1. 1.0 1.1 다시 말해, T 는 (const 한정이 가능한) 타입의 어떤 표현식으로부터도 생성 가능하거나 변환 가능하지 않습니다. std:: optional < U >

예외

3) T 의 생성자가 던지는 모든 예외를 throw합니다.
4) T 의 생성자가 던지는 모든 예외를 전파합니다. 다음과 같은
noexcept 명세를 가집니다:
noexcept ( std:: is_nothrow_move_constructible < T > :: value )
5-9) T 의 생성자가 던지는 모든 예외를 전파합니다.

추론 가이드

참고 사항

LWG 이슈 3836 이 해결되기 전에는, std:: optional < bool > std:: optional < U > 로부터 생성할 때 U bool 이 아닌 경우, 오버로드 ( 5,6 ) 대신 오버로드 ( 9 ) 가 선택되었습니다. 이는 오버로드 ( 5,6 ) T (이 경우 bool )가 std:: optional < U > 로부터 생성되거나 변환될 수 있는 경우에만 오버로드 해결에 참여했지만, std::optional::operator bool 가 모든 U 에 대해 변환을 가능하게 만들었기 때문입니다.

결과적으로, 생성된 std:: optional < bool > 는 항상 값을 포함합니다. 해당 값은 제공된 std:: optional < U > 객체가 값을 포함하는지 여부에 의해 결정되며, 포함된 값으로부터 직접 초기화된 bool 값 자체에 의해 결정되는 것은 아닙니다:

std::optional<bool> op_false(false);
std::optional<int> op_zero(0);
std::optional<int> from_bool(op_false); // OK: 0을 포함함 (false로부터 초기화됨)
std::optional<bool> from_int(op_zero);  // 결함 (LWG 3836): true를 포함함. 왜냐하면
                                        // op_zero가 값을 포함하고 있기 때문이며,
                                        // 해당 값으로 bool을 초기화하면 false가 되더라도
기능 테스트 매크로 표준 기능
__cpp_lib_optional 202106L (C++20)
(DR20)
완전한 constexpr ( 5,6 )

예제

#include <iostream>
#include <optional>
#include <string>
int main()
{
    std::optional<int> o1, // 비어 있음
                       o2 = 1, // rvalue로부터 초기화
                       o3 = o2; // 복사 생성자
    // std::string( initializer_list<CharT> ) 생성자 호출
    std::optional<std::string> o4(std::in_place, {'a', 'b', 'c'});
    // std::string( size_type count, CharT ch ) 생성자 호출
    std::optional<std::string> o5(std::in_place, 3, 'A');
    // 타입 추론 가이드를 사용하여 std::string으로부터 이동 생성
    std::optional o6(std::string{"deduction"});
    std::cout << *o2 << ' ' << *o3 << ' ' << *o4 << ' ' << *o5  << ' ' << *o6 << '\n';
}

출력:

1 1 abc AAA deduction

결함 보고서

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

DR 적용 대상 게시된 동작 수정된 동작
LWG 3836 C++17 std:: optional < bool > std:: optional < U > 에서 생성할 때
U bool 가 아닌 경우 오버로드 ( 9 ) 선택
이 경우 항상 변환
복사/이동 생성자를
선택하도록 수정
LWG 3886 C++17 오버로드 ( 9 ) 의 기본 템플릿 인자가 T 였음 std:: remove_cv_t < T > 로 변경
P0602R4 C++17 기본 생성자가 trivial해도
복사/이동 생성자가 trivial하지 않을 수 있음
triviality 전파를
요구하도록 수정
P2231R1 C++20 다른 std::optional 에서의 오버로드 ( 5,6 ) constexpr 가 아니었음 constexpr 로 변경

참고 항목

optional 객체를 생성합니다
(함수 템플릿)