Questions tagged [bit-cast]

14 questions
14
votes
1 answer

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 #include class A { int v; public: A(int u) : v(u) {} auto getter() const {…
Fedor
  • 17,146
  • 13
  • 40
  • 131
9
votes
0 answers

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 constexpr const char str[4] = "abc"; // error: constexpr variable 'x' must be initialized by a constant expression constexpr auto x =…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
3
votes
2 answers

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 test code can be found here:…
sh-
  • 941
  • 6
  • 13
3
votes
1 answer

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 with std::memcpy and it does not cause undefined…
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
3
votes
2 answers

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 T2 undefined_bit_cast(T1 v1) { T1 *p1 = &v1; T2 *p2 = (T2 *)p1; // Uh oh. T2 v2 =…
Craig Gidney
  • 17,763
  • 5
  • 68
  • 136
1
vote
2 answers

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"); const auto error2 = std::nan("2"); const auto error3 =…
Estin
  • 13
  • 3
1
vote
1 answer

Syntax for std::bit_cast to an array

#include #include struct A { int a[100]; }; struct B { short b[200]; }; void test(const A &in) { const auto x = std::bit_cast(in); // error: no matching function for call to 'bit_cast(const…
1
vote
1 answer

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 #include #include int main( ) { std::uint32_t var { 301 }; { std::ofstream…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
1
vote
1 answer

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 #include #include #include #include #include template
dave_thenerd
  • 448
  • 3
  • 10
0
votes
1 answer

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, pressure, etc.) stored in each cell in a grid. I would…
ander
  • 11
  • 3
0
votes
0 answers

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 address 0x100). Therefore I tried std::bit_cast-ing to…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
0
votes
1 answer

How to let gcc optimize std::bit_cast with std::move?

Consider below code: #include #include #include struct A { uint32_t a[100]; }; struct B { uint16_t b[200]; }; void test(const A&); void foo() { B tmp; test(std::bit_cast(std::move(tmp))); } void bar() { B…
0
votes
0 answers

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(&obj), sizeof(obj)) out_file.write(reinterpret_cast(&obj), sizeof(obj)) Now, is there any guarantee within the standard…
0
votes
1 answer

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: #include template struct…
markt1964
  • 2,638
  • 2
  • 22
  • 54