reinterpret-cast Questions

8

Solved

I wonder what's the equivalent of C++'s reinterpret_cast in C#!? Here's my sample: class Base { protected int counter = 0; } class Foo : Base { public int Counter { get { return counter; } ...
Meekins asked 21/10, 2013 at 14:51

0

I am writing a C++ tagged pointer. I have a question about whether the operations I use to implement its basic functionality cause undefined behavior: When constructing a tagged pointer given a po...
Poeticize asked 23/1, 2024 at 3:16

5

Solved

Is it possible in C# to turn a bool into a byte or int (or any integral type, really) without branching? In other words, a ternary is not good enough: var myInt = myBool ? 1 : 0; // could compile t...
Contrapuntal asked 19/11, 2019 at 13:15

1

This question refers to the current C++20 draft. The quoted passages have been slightly modified from previous standard iterations, but not in relevant ways as far as I know. I am looking for clar...
Vyse asked 27/2, 2020 at 17:56

0

This question results from thinking about my answer in Given two objects of different types and their relative location in memory, can I derive a pointer to one object from a pointer to the other?....
Amethist asked 26/4, 2023 at 22:4

2

Solved

There are a few questions and answers on the site already concerning pointer-interconvertibility of structs and their first member variable, as well as structs and their first public base. This que...

9

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?
Planogamete asked 21/11, 2008 at 22:39

6

Solved

I've always considered C++ to be one of the most strongly typed languages out there. So I was quite shocked to see Table 3 of this paper state that C++ is weakly typed. Apparently, C and C++ are c...
Malevolent asked 5/11, 2014 at 9:23

1

Solved

From what I understand, casting function pointers to different types is allowed by the C++ standard (as long as one never invokes them): int my_func(int v) { return v; } int main() { using from_t...
Apul asked 15/11, 2022 at 13:41

3

Solved

Reading https://en.cppreference.com/w/cpp/language/reinterpret_cast I wonder what are use-cases of reinterpret_cast that are not UB and are used in practice? The above description contains many cas...
Zionism asked 27/10, 2022 at 5:10

1

Solved

I’ve searched Stack Overflow for an answer, but I get nothing specific to this problem: only general cases about use of various types of cast operators. So, the case in point is when retrieving a f...
Hagbut asked 17/9, 2019 at 11:29

1

Solved

I am reading a book on writing modern C++ code for microcontrollers which is named "Real time C++". I am trying to write the codes in the book myself. However, while copying the code from...
Divebomb asked 3/1, 2022 at 20:5

2

Solved

Consider: float const& f = 5.9e-44f; int const i = (int&) f; Per expr.cast/4 this should be considered as, in order: a const_­cast, a static_­cast, a static_­cast followed by a const_­ca...
Milzie asked 26/3, 2021 at 12:13

1

Solved

I'm using C# (.NET 5). Imagine I have a class that stores an array of structures (say, floats): public class StoresArray { private float[] floats; } This class's data is loaded from a serialized ...
Lamellar asked 10/12, 2021 at 19:29

3

Solved

I recently learned that it is Undefined Behavior to reinterpret a POD as a different POD by reinterpret_casting its address. So I'm just wondering what a potential use-case of reinterpret_cast migh...
Fanciful asked 29/7, 2021 at 12:38

1

I have a matrix class with a transposedView() method that I have been using for years as a "zero overhead" conversion between row and column vectors. template<int M, int N=M, typename ...
Beckwith asked 29/7, 2021 at 1:36

5

Solved

Assume I'm doing some socket programming: struct sockaddr_in sa; inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr)); auto *resa = reinterpret_cast<struct sockaddr*>(&sa); bind(sfd, resa...
Poling asked 27/5, 2017 at 20:37

2

Is the following code UB? int i = 5; void *p = &i; int* &r = reinterpret_cast<int* &>(p); int* p2 = r; Please note I do not dereference pointer.
Sandell asked 28/6, 2021 at 11:25

1

Solved

Given the following conditions: struct A { int a; }; struct B { int b; }; int main() { A a {1}; A* p = &a; Does casting with static_cast and with reinterpret_cast via void* give the same...
Counterblow asked 25/6, 2021 at 21:35

3

Solved

In c++11, a constexpr expression cannot contain reinterpret casts. So for instance, if one wanted to manipulate the bits in a floating point number, say to find the mantissa of the number: constex...
Alvarado asked 5/10, 2014 at 8:11

0

One of the preconditions on std::launder requires that object is within its lifetime. I assume that it is a necessary condition for being able to dereference the element. Does it mean, that if I ob...
Illfavored asked 24/3, 2021 at 15:14

2

When implementing certain data structures in C++ one needs to be able to create an array that has uninitialized elements. Because of that, having buffer = new T[capacity]; is not suitable, as new ...

7

Solved

My understanding is that C++ reinterpret_cast and C pointer cast is a just a compile-time functionality and that it has no performance cost at all. Is this true?
Fireplug asked 26/8, 2010 at 12:57

7

Solved

Here's what I want to do: const int64_t randomIntNumber = reinterpret_cast<int64_t> (randomUintNumber); Where randomUintNumber is of type uint64_t. The error is (MSVC 2010): error C244...
Perspective asked 31/1, 2013 at 10:13

2

GodBolt Consider the following code snippet: using A = void(*)(int); A foo(const void* ptr) { return reinterpret_cast<A>(ptr); } GCC 10 likes this just fine. clang++-10, however, says thi...

© 2022 - 2025 — McMap. All rights reserved.