GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::as_bytes,std::as_writable_bytes(3) C++ Standard Libary std::as_bytes,std::as_writable_bytes(3)

std::as_bytes,std::as_writable_bytes - std::as_bytes,std::as_writable_bytes


Defined in header <span>
template< class T, std::size_t N>
std::span<const std::byte, S/* see below */> as_bytes(std::span<T, (1) (since C++20)
N> s) noexcept;
template< class T, std::size_t N>
std::span<std::byte, S/* see below */> (2) (since C++20)
as_writable_bytes(std::span<T, N> s) noexcept;


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.


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()}.

// Run this code


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


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


byte the byte type
(C++17) (enum)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.