tuple_ignore

tuple_ignore (std::ignore)

이 페이지는 C++ 표준 라이브러리의 std::ignore 객체에 대해 설명해요. std::ignorestd::tie와 함께 사용할 때 튜플의 특정 요소를 무시하기 위한 자리 표시자로 활용돼요. 이 객체는 임의의 값을 할당받을 수 있지만, 그 값을 실제로 저장하지는 않아요.

출처: cppreference

본문

헤더 및 선언

정의된 헤더
<tuple>
<utility>
const /*ignore-type*/ ignore ; (1) (since C++11) (until C++14)
constexpr /*ignore-type*/ ignore ; (since C++14) (inline since c++17)
struct /*ignore-type*/ { template < class T > const /*ignore-type*/ & operator = ( const T & ) const noexcept { return * this ; } }; (2) (since C++11) (until C++14) ( exposition only* )
struct /*ignore-type*/ { template < class T > constexpr const /*ignore-type*/ & operator = ( const T & ) const noexcept { return * this ; } }; (since C++14) ( exposition only* )

참고 사항

void 표현식이나 휘발성 비트 필드 값은 std::ignore에 할당할 수 없어요.

std::ignorestd::tuple을 풀어낼 때 std::tie와 함께 사용하도록 설계되었어요. 사용하지 않는 인자의 자리 표시자로 쓰이지만, 원치 않는 할당을 무시하는 용도로도 사용할 수 있어요.

일부 코딩 가이드에서는 할당이 필요하지 않더라도 [[nodiscard]] 함수의 반환값으로 인한 경고를 피하기 위해 std::ignore를 사용하라고 권장해요.

할당이 필요 없는 값을 무시할 때는 void로 캐스팅할 수 있어요. 이름은 있지만 값이 사용되지 않는 변수의 경우, 해당 변수를 void로 캐스팅하거나 [[maybe_unused]]로 선언할 수 있어요.

예제

  • [[nodiscard]] 함수와 함께 std::ignore를 사용하는 방법을 보여줘요.
  • std::set::insert()가 반환하는 std::pair<iterator, bool>을 풀어내되, bool 값만 저장해요.
#include <iostream>
#include <set>
#include <string>
#include <tuple>

[[nodiscard]] int dontIgnoreMe()
{
    return 42;
}

int main()
{
    std::ignore = dontIgnoreMe();

    std::set<std::string> set_of_str;
    if (bool inserted{false};
        std::tie(std::ignore, inserted) = set_of_str.insert("Test"),
        inserted)
        std::cout << "Value was inserted successfully.\n";
}

출력:

Value was inserted successfully.

결함 보고

다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.

DR 적용 대상 발표된 동작 올바른 동작
LWG 2773 C++14 std::tuple은 constexpr이 되었지만 std::ignore는 아직 아니었음 constexpr로 만듦
P2968R2 C++11 std::tie 외부에서의 std::ignore 동작이 공식적으로 명시되지 않았음 완전히 명시함

같이 보기

tie (C++11) lvalue 참조의 튜플을 만들거나 튜플을 개별 객체로 풀어내는 함수 템플릿

더 알아보기 (Learn more)

cppreference