Namespaces
Variants

std:: as_bytes, std:: as_writable_bytes

From cppreference.net
헤더 파일에 정의됨 <span>
template < class T, std:: size_t N >

std:: span < const std:: byte , S /* see below */ >

as_bytes ( std:: span < T, N > s ) noexcept ;
(1) (C++20부터)
template < class T, std:: size_t N >

std:: span < std:: byte , S /* see below */ >

as_writable_bytes ( std:: span < T, N > s ) noexcept ;
(2) (C++20부터)

스팬 s 의 요소들에 대한 객체 표현의 뷰를 획득합니다.

만약 N std::dynamic_extent 라면, 반환된 span S 의 extent 또한 std::dynamic_extent 입니다; 그렇지 않으면 sizeof ( T ) * N 입니다.

as_writable_bytes std:: is_const_v < T > false 인 경우에만 오버로드 해결에 참여합니다.

반환값

1) { reinterpret_cast < const std:: byte * > ( s. data ( ) ) , s. size_bytes ( ) } 로 구성된 span.
2) { reinterpret_cast < std:: byte * > ( s. data ( ) ) , s. size_bytes ( ) } 로 구성된 span.

예제

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <span>
void print(float const x, std::span<const std::byte> const bytes)
{
    std::cout << std::setprecision(6) << std::setw(8) << x << " = { "
              << std::hex << std::uppercase << std::setfill('0');
    for (auto const b : bytes)
        std::cout << std::setw(2) << std::to_integer<int>(b) << ' ';
    std::cout << std::dec << "}\n";
}
int main()
{
    /* mutable */ float data[1]{3.141592f};
    auto const const_bytes = std::as_bytes(std::span{data});
    print(data[0], const_bytes);
    auto const writable_bytes = std::as_writable_bytes(std::span{data});
    // IEEE 754 부동소수점 표준에 따라 MSB인 부호 비트를 변경합니다.
    writable_bytes[3] |= std::byte{0B1000'0000};
    print(data[0], const_bytes);
}

가능한 출력:

 3.14159 = { D8 0F 49 40 }
-3.14159 = { D8 0F 49 C0 }

참고 항목

주어진 저장소에서 객체 표현을 재사용하여 객체를 암묵적으로 생성함
(함수 템플릿)
(C++17)
바이트 타입
(열거형)