Namespaces
Variants

std::empty

cppreference.net에서
 
 
반복자 라이브러리
반복자 컨셉
반복자 기본 요소
알고리즘 컨셉 및 유틸리티
간접 호출 가능 컨셉
공통 알고리즘 요구 사항
(C++20)
(C++20)
(C++20)
유틸리티
(C++20)
반복자 어댑터
범위 접근
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
헤더 <array>
에 정의됨<deque>
헤더 <flat_map>
 (C++23부터)
에 정의됨<flat_set>
 헤더
(C++23부터) <forward_list>
에 정의됨<inplace_vector>
 헤더
에 정의됨<iterator>
헤더 <list>
에 정의됨<map>
헤더 <optional>
 (C++26부터)
에 정의됨<regex>
헤더 <set>
에 정의됨<span>
 헤더
(C++20부터) <stacktrace>
 에 정의됨
헤더 <string>
(C++23부터) <string_view>
에 정의됨<unordered_map>
에 정의됨<valarray>
헤더 <vector>
template< class C >
constexpr auto empty( const C& c ) noexcept(noexcept(c.empty()))
    -> decltype(c.empty());
에 정의됨 (C++17부터)
template< class T, std::size_t N >
constexpr bool empty( const T (&array)[N] ) noexcept;
(2) (C++17부터)

주어진 범위가 비어 있는지 여부를 반환한다.

1) c.empty()을(를) 반환한다.
2) false을(를) 반환한다.

매개변수

c - empty 멤버 함수를 가진 컨테이너 또는 뷰
array - 임의 타입의 배열
il - std::initializer_list

반환값

1) c. empty ( )
2) false
3) il. size ( ) == 0

예외

1) 구현에서 정의된 예외를 throw할 수 있습니다.

참고 사항

std::initializer_list 에 대한 오버로드는 empty 멤버 함수를 가지고 있지 않기 때문에 필요합니다.

기능 테스트 매크로 값 표준 기능
__cpp_lib_nonmember_container_access 201411L (C++17) std::size() , std::data() , 및 std::empty()

가능한 구현

첫 번째 버전
template<class C>
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty())
{
    return c.empty();
}
두 번째 버전
template<class T, std::size_t N>
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept
{
    return false;
}
세 번째 버전
template<class E>
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept
{
    return il.size() == 0;
}

예제

#include <iostream>
#include <vector>
template<class T>
void print(const T& container)
{
    if (std::empty(container))
        std::cout << "Empty\n";
    else
    {
        std::cout << "Elements:";
        for (const auto& element : container)
            std::cout << ' ' << element;
        std::cout << '\n';
    }
}
int main()
{
    std::vector<int> c = {1, 2, 3};
    print(c);
    c.clear();
    print(c);
    int array[] = {4, 5, 6};
    print(array);
    auto il = {7, 8, 9};
    print(il);
}

출력:

Elements: 1 2 3
Empty
Elements: 4 5 6
Elements: 7 8 9

결함 보고

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

DR 적용 대상 게시된 동작 올바른 동작
P3016R6 C++17 1) 비멤버 std::empty 오버로드가 std::initializer_list
에 제공되었습니다. 2) std::empty은 예외 명세를 전파할 필요가 없었습니다.
1) 제거됨
2) 필요함

참고 항목

범위가 비어 있는지 여부를 확인합니다
(커스터마이제이션 포인트 객체)