Namespaces
Variants

std:: endian

From cppreference.net
Utilities library
헤더 파일에 정의됨 <bit>
enum class endian

{
little = /* implementation-defined */ ,
big = /* implementation-defined */ ,
native = /* implementation-defined */ ,

} ;
(C++20부터)

모든 endianness 를 나타냅니다 scalar types 의:

  • 모든 스칼라 타입이 리틀 엔디언인 경우, std :: endian :: native std :: endian :: little 와 같습니다.
  • 모든 스칼라 타입이 빅 엔디언인 경우, std :: endian :: native std :: endian :: big 와 같습니다.

코너 케이스 플랫폼도 지원됩니다:

  • 모든 스칼라 타입의 sizeof 1 와 같다면, 엔디언은 중요하지 않으며 세 값 모두 std :: endian :: little , std :: endian :: big , 그리고 std :: endian :: native 가 동일합니다.
  • 플랫폼이 혼합 엔디언을 사용하는 경우, std :: endian :: native std :: endian :: big std :: endian :: little 도 아닙니다.

목차

가능한 구현

enum class endian
{
#if defined(_MSC_VER) && !defined(__clang__)
    little = 0,
    big    = 1,
    native = little
#else
    little = __ORDER_LITTLE_ENDIAN__,
    big    = __ORDER_BIG_ENDIAN__,
    native = __BYTE_ORDER__
#endif
};

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_endian 201907L (C++20) std :: endian

예제

#include <bit>
#include <iostream>
int main()
{
    if constexpr (std::endian::native == std::endian::big)
        std::cout << "big-endian\n";
    else if constexpr (std::endian::native == std::endian::little)
        std::cout << "little-endian\n";
    else
        std::cout << "mixed-endian\n";
}

가능한 출력:

mixed-endian

참고 항목

(C++23)
주어진 정수 값의 바이트 순서를 반전시킴
(함수 템플릿)