Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/span/as bytes"

From cppreference.com
< cpp‎ | container‎ | span
m (See also: reuse dsc.)
m (fmt)
 
Line 4: Line 4:
 
{{dcl header|span}}
 
{{dcl header|span}}
 
{{dcl|num=1|since=c++20|1=
 
{{dcl|num=1|since=c++20|1=
template< class T, std::size_t N>
+
template< class T, std::size_t N >
 
std::span<const std::byte, S/* see below */>
 
std::span<const std::byte, S/* see below */>
 
     as_bytes( std::span<T, N> s ) noexcept;
 
     as_bytes( std::span<T, N> s ) noexcept;
 
}}
 
}}
 
{{dcl|num=2|since=c++20|1=
 
{{dcl|num=2|since=c++20|1=
template< class T, std::size_t N>
+
template< class T, std::size_t N >
 
std::span<std::byte, S/* see below */>
 
std::span<std::byte, S/* see below */>
 
     as_writable_bytes( std::span<T, N> s ) noexcept;
 
     as_writable_bytes( std::span<T, N> s ) noexcept;
Line 26: Line 26:
  
 
===Example===
 
===Example===
{{example|code=
+
{{example
 +
|code=
 
#include <cstddef>
 
#include <cstddef>
 
#include <iomanip>
 
#include <iomanip>
Line 45: Line 46:
 
     /* mutable */ float data[1]{3.141592f};
 
     /* mutable */ float data[1]{3.141592f};
  
     auto const const_bytes = std::as_bytes(std::span{ data });
+
     auto const const_bytes = std::as_bytes(std::span{data});
  
 
     print(data[0], const_bytes);
 
     print(data[0], const_bytes);
  
     auto const writable_bytes = std::as_writable_bytes(std::span{ data });
+
     auto const writable_bytes = std::as_writable_bytes(std::span{data});
  
 
     // Change the sign bit that is the MSB (IEEE 754 Floating-Point Standard).
 
     // Change the sign bit that is the MSB (IEEE 754 Floating-Point Standard).
     writable_bytes[3] {{!=}} std::byte{ 0B1000'0000 };
+
     writable_bytes[3] {{!=}} std::byte{0B1000'0000};
  
 
     print(data[0], const_bytes);
 
     print(data[0], const_bytes);
 
}
 
}
|p=true<!-- requires IEEE-754 format and little endian -->
+
|p=true<!--requires IEEE-754 format and little endian-->
 
|output=<nowiki/>
 
|output=<nowiki/>
 
  3.14159 = { D8 0F 49 40 }
 
  3.14159 = { D8 0F 49 40 }

Latest revision as of 00:01, 28 September 2023

 
 
 
 
Defined in header <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) (since 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) (since C++20)

Obtains a view to the object representation of the elements of the span s.

If N is std::dynamic_extent, the extent of the returned span S is also std::dynamic_extent; otherwise it is sizeof(T) * N.

as_writable_bytes only participates in overload resolution if std::is_const_v<T> is false.

[edit] Return value

1) A span constructed with {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()}.
2) A span constructed with {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()}.

[edit] Example

#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});
 
    // Change the sign bit that is the MSB (IEEE 754 Floating-Point Standard).
    writable_bytes[3] |= std::byte{0B1000'0000};
 
    print(data[0], const_bytes);
}

Possible output:

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

[edit] See also

implicitly creates objects in given storage with the object representation reused
(function template) [edit]
(C++17)
the byte type
(enum) [edit]