26 questions
0
votes
0
answers
83
views
Modifying object representation in presence of padding bits
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 ...
4
votes
1
answer
167
views
Can any char be converted in bool using std::bit_cast?
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>
...
6
votes
1
answer
128
views
Can std::bit_cast convert to or from std::nullptr_t type?
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 ...
12
votes
1
answer
412
views
Why does bit cast require both types to be trivially-copyable?
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);
...
1
vote
1
answer
109
views
Allowing different fundamental types to share the same memory location by using std::bit_cast instead of std::variant or union?
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 ...
2
votes
2
answers
390
views
How to use std::bit_cast on an array pointer type argument
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;
...
5
votes
1
answer
182
views
Can std::bit_cast be applied to an empty object?
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 ...
6
votes
1
answer
400
views
std::bit_cast padding and undefined behavior
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 ...
0
votes
3
answers
416
views
Can i use placement new as a bit_cast?
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 ...
3
votes
2
answers
1k
views
Trying to use std::bit_cast with a bitfield struct. Why is it not constexpr?
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 ...
0
votes
1
answer
236
views
Design choices for writing efficient numerical solvers in c++: Type punning
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, ...
1
vote
2
answers
136
views
Bitwise comparing NaN's in C++
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");
...
1
vote
0
answers
769
views
std::bit_cast to pointer type not useable in constexpr context
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 ...
1
vote
1
answer
654
views
Syntax for std::bit_cast to an array
#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); // ...
0
votes
1
answer
247
views
How to let gcc optimize std::bit_cast with std::move?
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 ...
0
votes
0
answers
81
views
Does reinterpret_cast together with binary input/output always leads to undefined behavior?
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*>(&...
1
vote
1
answer
2k
views
std::bit_cast vs reinterpret_cast in file I/O
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(...
4
votes
1
answer
2k
views
Can bit_cast do the same thing as memcpy to store a pointer into an array of shorts without undefined behaviour?
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 ...
1
vote
1
answer
699
views
How can I convert ANY user defined type to an std::bitset?
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&...
14
votes
1
answer
616
views
Is it valid to create closure (lambda) objects using `std::bit_cast` in C++20?
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 ...
9
votes
2
answers
5k
views
Safe equivalent of std::bit_cast in C++11
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 *...
13
votes
1
answer
688
views
Why can't I std::bit_cast the contents of a string literal?
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 ...
0
votes
1
answer
554
views
What, if any, additional UB will bit_cast have beyond memcpy?
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:
#...
24
votes
3
answers
6k
views
Can you std::bit_cast to a std::array to obtain the bytes of an object?
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 ...
17
votes
2
answers
6k
views
Any useful difference between std::bit_cast and std::start_lifetime_as?
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, ...
84
votes
2
answers
35k
views
Why was std::bit_cast added, if reinterpret_cast could do the same?
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 ...