Skip to main content
Filter by
Sorted by
Tagged with
0 votes
0 answers
83 views

This question is a follow-up of std::bit_cast padding and undefined behavior. This "revival" is motivated by my answer to Accessing object storage where I proposed a function to modify a ...
Oersted's user avatar
  • 3,834
4 votes
1 answer
167 views

May one convert a char with the value not \0 and not \1 into bool using std::bit_cast? And if yes, what will the value of the resulting bool: true or false? For example, #include <bit> ...
Fedor's user avatar
  • 24.7k
6 votes
1 answer
128 views

Is it prohibited to use std::bit_cast for conversion to or from std::nullptr_t = decltype(nullptr) type? And if it is permitted, 1) must the result of std::bit_cast be the same as static_cast, 2) must ...
Fedor's user avatar
  • 24.7k
12 votes
1 answer
412 views

Why does C++ std::bit_cast require both To and From to be trivially-copyable? For example: From from{}; To to; static_assert(sizeof to == sizeof from); std::memcpy(&to, &from, sizeof to); ...
lobelk's user avatar
  • 531
1 vote
1 answer
109 views

It's possible to store ints or floats in the same memory location using std::bit_cast (including ints larger than 2^24 i.e. they can't be stored as whole numbers in floats because the mantissa has too ...
greenlagoon's user avatar
2 votes
2 answers
390 views

Is it possible to pass address of the first element of an array as argument to std::bit_cast. #include <bit> #include <iostream> struct A { int a; int b; int c; int d; ...
Harry's user avatar
  • 4,146
5 votes
1 answer
182 views

Is it allowed to apply std::bit_cast to an object of empty class, converting it to some not-empty class of the same size? And especially is it allowed to do in a constant expression? I was surprised ...
Fedor's user avatar
  • 24.7k
6 votes
1 answer
400 views

I want to know how to use std::bit_cast in a well-defined way, noticeably in presence of indetermined bits. When is std::bit_cast usage defined behavior, when is it undefined behavior? Thus I need a ...
Oersted's user avatar
  • 3,834
0 votes
3 answers
416 views

I'm interested in ways to get around the restriction on binary type conversion, because bit_cast uses copying to a variable on the stack, which is not very fast. But as far as I know, trivial types do ...
yatanai's user avatar
  • 45
3 votes
2 answers
1k views

I'm trying to get a bitmask from a bitfield struct, at compile time. One of the tricks that I tried, which looks promising to me, is using std::bit_cast, because it is supposed to be constexpr. My ...
sh-'s user avatar
  • 1,031
0 votes
1 answer
236 views

I'm writing a numerical fluid solver in C++ as a hobby project. I will try to explain what I want to accomplish in a simplified manner. The solver has multiple flow variables (density, velocity, ...
ander's user avatar
  • 41
1 vote
2 answers
136 views

I have a function that returns a double. Any real number is a valid output. I'm using nan's to signal errors. I am error checking this way. double foo(); const auto error1 = std::nan("1"); ...
Estin's user avatar
  • 13
1 vote
0 answers
769 views

Reading thru https://github.com/isocpp/CppCoreGuidelines/issues/1517 I got the idea to get rid of the non-constexpr reinterpret_cast from my µC code (e.g. C could be a µC internal peripheral on ...
wimalopaan's user avatar
  • 5,552
1 vote
1 answer
654 views

#include <bit> #include <array> struct A { int a[100]; }; struct B { short b[200]; }; void test(const A &in) { const auto x = std::bit_cast<short[200]>(in); // ...
Home of the Brave's user avatar
0 votes
1 answer
247 views

Consider below code: #include <cstdint> #include <bit> #include <utility> struct A { uint32_t a[100]; }; struct B { uint16_t b[200]; }; void test(const A&); void foo() { B ...
Home of the Brave's user avatar
0 votes
0 answers
81 views

When working binary input/output, we often see/write code similar to this in_file.read(reinterpret_cast<char *>(&obj), sizeof(obj)) out_file.write(reinterpret_cast<const char*>(&...
Myrddin Krustowski's user avatar
1 vote
1 answer
2k views

How should one cast pointers to char*/const char*? Using reinterpret_cast? Or probably std::bit_cast? A simple example: #include <iostream> #include <fstream> #include <bit> int main(...
digito_evo's user avatar
  • 3,735
4 votes
1 answer
2k views

While there might be an XY issue that led me here, I am curious about the limits of the new bit_cast in C++20, and this seems a good illustration of it. Note that there is nothing inherently wrong ...
Glenn Teitelbaum's user avatar
1 vote
1 answer
699 views

What I want to achieve is a means of converting any arbitrarily sized and formatted type to an std::bitset. Like this: #include<bitset> #include<bit> #include<cstdlib> #include&...
dave_thenerd's user avatar
14 votes
1 answer
616 views

A colleague showed me a C++20 program where a closure object is virtually created using std::bit_cast from the value that it captures: #include <bit> #include <iostream> class A { int ...
Fedor's user avatar
  • 24.7k
9 votes
2 answers
5k views

C++20 introduced std::bit_cast for treating the same bits as if they were a different type. So, basically it does this: template <typename T1, typename T2> T2 undefined_bit_cast(T1 v1) { T1 *...
Craig Gidney's user avatar
  • 18.4k
13 votes
1 answer
688 views

While experimenting with compile-time string manipulation, I've encountered a strange phenomenon: #include <bit> constexpr char str[4] = "abc"; // error: constexpr variable 'x' must ...
Jan Schultke's user avatar
  • 43.7k
0 votes
1 answer
554 views

I have a class whose purpose is to move data which might have alignment restrictions to or from a serialized memory buffer where the data is not aligned. I have set and get handlers as follows: #...
markt1964's user avatar
  • 2,896
24 votes
3 answers
6k views

In his recent talk “Type punning in modern C++” Timur Doumler said that std::bit_cast cannot be used to bit cast a float into an unsigned char[4] because C-style arrays cannot be returned from a ...
Evg's user avatar
  • 26.6k
17 votes
2 answers
6k views

std::bit_cast is apparently being introduced in c++20. and std::start_lifetime_as is proposed for c++23 (from P0593R5). As they appear to both require that the datatypes involved be trivial anyways, ...
markt1964's user avatar
  • 2,896
84 votes
2 answers
35k views

std::bit-cast was introduced in the C++20 standard. I know that reinterpret_cast is not suitable for this job due to type aliasing rules. However, why did they choose not to extend reinterpret_cast to ...
bogdan tudose's user avatar