Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types/endian"

From cppreference.com
< cpp‎ | types
(+)
 
m (+' ')
 
(20 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|endian}}
 
{{cpp/title|endian}}
{{cpp/types/navbar}}
+
{{cpp/numeric/navbar}}
{{dcl begin}}
+
{{ddcl|header=bit|since=c++20|1=
{{dcl header | type_traits}}
+
{{dcl | since=c++20 | num=1 | 1=
+
 
enum class endian
 
enum class endian
 
{
 
{
 
     little = /*implementation-defined*/,
 
     little = /*implementation-defined*/,
 
     big    = /*implementation-defined*/,
 
     big    = /*implementation-defined*/,
     native = /*implementation-defined*/
+
     native = /*implementation-defined*/,
 
};
 
};
 
}}
 
}}
{{dcl end}}
 
  
Indicates the endianness of all [[cpp/language/type|scalar types]]:
+
Indicates the {{enwiki|Endianness#Overview|endianness}} of all [[cpp/language/type|scalar types]]:
  
* If all scalar types are little-endian, {{tt|std::endian::native}} equals {{tt|std::endian::little}}
+
* If all scalar types are little-endian, {{c|std::endian::native}} equals {{c|std::endian::little}}.
* If all scalar types are big-endian, {{tt|std::endian::native}} equals {{tt|std::endian::big}}
+
* If all scalar types are big-endian, {{c|std::endian::native}} equals {{c|std::endian::big}}.
  
 
Corner case platforms are also supported:
 
Corner case platforms are also supported:
* If all scalar types have sizeof equal to 1, endianness does not matter and all three values, {{tt|std::endian::little}}, {{tt|std::endian::big}}, and {{tt|std::endian::native}} are the same
+
* If all scalar types have {{tt|sizeof}} equal to {{c|1}}, endianness does not matter and all three values, {{c|std::endian::little}}, {{c|std::endian::big}}, and {{c|std::endian::native}} are the same.
* If the platform uses mixed endian, {{tt|std::endian::native}} does not equal either {{tt|std::endian::big}} nor {{tt|std::endian::little}}
+
* If the platform uses mixed endian, {{c|std::endian::native}} equals neither {{c|std::endian::big}} nor {{c|std::endian::little}}.
  
 
===Possible implementation===
 
===Possible implementation===
Line 26: Line 23:
 
enum class endian
 
enum class endian
 
{
 
{
#ifdef _WIN32
+
#if defined(_MSC_VER) && !defined(__clang__)
 
     little = 0,
 
     little = 0,
 
     big    = 1,
 
     big    = 1,
Line 32: Line 29:
 
#else
 
#else
 
     little = __ORDER_LITTLE_ENDIAN__,
 
     little = __ORDER_LITTLE_ENDIAN__,
     big    = __ORDER_BIG_ENDIAN__
+
     big    = __ORDER_BIG_ENDIAN__,
     native = __BYTE_ORDER__,
+
     native = __BYTE_ORDER__
 
#endif
 
#endif
 
};
 
};
 
}}
 
}}
 +
 +
===Notes===
 +
{{feature test macro|__cpp_lib_endian|std=C++20|value=201907L|{{c|std::endian}}}}
  
 
===Example===
 
===Example===
<!--
+
{{example
if (std::endian::native == std::endian::big)
+
|code=
    // handle big endian
+
#include <bit>
else if (std::endian::native == std::endian::little)
+
#include <iostream>
    // handle big endian
+
 
else
+
int main()
    // handle mixed endian
+
{
-->
+
    if constexpr (std::endian::native == std::endian::big)
{{example}}
+
        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";
 +
}
 +
|p=true
 +
|output=
 +
mixed-endian
 +
}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/numeric/dsc byteswap}}
 +
{{dsc end}}
  
[[de:cpp/types/endian]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/types/endian]]
+
[[fr:cpp/types/endian]]
+
[[it:cpp/types/endian]]
+
[[ja:cpp/types/endian]]
+
[[pt:cpp/types/endian]]
+
[[ru:cpp/types/endian]]
+
[[zh:cpp/types/endian]]
+

Latest revision as of 10:58, 5 July 2023

 
 
 
Defined in header <bit>
enum class endian

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

};
(since C++20)

Indicates the endianness of all scalar types:

  • If all scalar types are little-endian, std::endian::native equals std::endian::little.
  • If all scalar types are big-endian, std::endian::native equals std::endian::big.

Corner case platforms are also supported:

  • If all scalar types have sizeof equal to 1, endianness does not matter and all three values, std::endian::little, std::endian::big, and std::endian::native are the same.
  • If the platform uses mixed endian, std::endian::native equals neither std::endian::big nor std::endian::little.

Contents

[edit] Possible implementation

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
};

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_endian 201907L (C++20) std::endian

[edit] Example

#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";
}

Possible output:

mixed-endian

[edit] See also

(C++23)
reverses the bytes in the given integer value
(function template) [edit]