reinterpret-cast

Does reinterpret_casting std::aligned_storage* to T* without std::launder violate strict-aliasing rules? [duplicate]

南笙酒味 提交于 2019-12-01 01:08:48
This question already has an answer here: Does this really break strict-aliasing rules? 2 answers The following example comes from std::aligned_storage page of cppreference.com: #include <iostream> #include <type_traits> #include <string> template<class T, std::size_t N> class static_vector { // properly aligned uninitialized storage for N T's typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N]; std::size_t m_size = 0; public: // Create an object in aligned storage template<typename ...Args> void emplace_back(Args&&... args) { if( m_size >= N ) // possible error handling throw

reinterpret_cast from object to first member

浪尽此生 提交于 2019-11-30 22:20:52
I was looking at this answer and I was wondering if casting an object to its first member with reinterpret_cast and using the result could be safe in C++. Let's assume that we have a class A, a class B and an instance b of B: class A{ public: int i; void foo(){} }; class B{ public: A a; }; B b; Question 1: Is it safe to use b.a like this: reinterpret_cast<A*>(&b)->foo() ? Note: In the general case we suppose that the class and its member are both standard layout. My lecture of the available references on reinterpret_cast tells me such usage should be authorized as there is no aliasing

Why is it important to use static_cast instead of reinterpret_cast here?

时间秒杀一切 提交于 2019-11-30 19:24:49
At a reply of a blog post of Raymond Chen , A questioner pointed out Raymond, I believe the C++ example is not correct since the position of the base class subobject in the derived class is unspecified according to ISO C++ 2003 Standard (10-3, page 168), and you assume that the base class subobject is always at the beginning. The C example would be fine in C++ too, so I'd stick with it. Raymond replied [The code does not make this assumption. That's why it's important to use static_cast instead of reinterpret_cast. Try it: Add a virtual method to OVERLAPPED (so a vtable goes in front) and

Safety of invalid downcast using static_cast (or reinterpret_cast) for inheritance without added members

為{幸葍}努か 提交于 2019-11-30 18:55:14
问题 I was wondering what the standard says about the safety of the following code: class A { int v; }; class B: public A { }; // no added data member A a; B& b = static_cast<B&>(a); Obviously the runtime type of a is A , not B , so the cast is not really type safe. However, since there was no member added and nothing is virtual, IMO the memory layout of the classes should be the same and this should work (maybe it would be nicer to write reinterpret_cast to indicate this behaviour?). My guess

reinterpret_cast from object to first member

丶灬走出姿态 提交于 2019-11-30 18:26:27
问题 I was looking at this answer and I was wondering if casting an object to its first member with reinterpret_cast and using the result could be safe in C++. Let's assume that we have a class A, a class B and an instance b of B: class A{ public: int i; void foo(){} }; class B{ public: A a; }; B b; Question 1: Is it safe to use b.a like this: reinterpret_cast<A*>(&b)->foo() ? Note: In the general case we suppose that the class and its member are both standard layout. My lecture of the available

Does reinterpret_cast lead to undefined behavior?

橙三吉。 提交于 2019-11-30 13:41:54
I have a class template A which contains a container of pointers ( T* ): template <typename T> class A { public: // ... private: std::vector<T*> data; }; and a bunch of functions like: void f(const A<const T>&); void g(const A<const T>&); Is it OK to call these functions via a cast from A<const T> to A<T> ? A<double> a; ... auto& ac = reinterpret_cast<const A<const double>&>(a); f(ac); I'm pretty sure that this code has undefined behaviour. Is it dangerous to use such conversions in real life? As A<double> and A<const double> are unrelated types, it's actually unspecified (originally I thought

Is it undefined behavior to `reinterpret_cast` a `T*` to `T(*)[N]`?

随声附和 提交于 2019-11-30 12:33:43
问题 Consider the following scenario: std::array<int, 8> a; auto p = reinterpret_cast<int(*)[8]>(a.data()); (*p)[0] = 42; Is this undefined behavior ? I think it is. a.data() returns a int* , which is not the same as int(*)[8] The type aliasing rules on cppreference seem to suggest that the reinterpret_cast is not valid As a programmer, I know that the memory location pointed by a.data() is an array of 8 int objects Is there any rule I am missing that makes this reinterpret_cast valid? 回答1: An

Problem casting STL complex<double> to fftw_complex

删除回忆录丶 提交于 2019-11-30 12:25:33
问题 The FFTW manual says that its fftw_complex type is bit compatible to std::complex<double> class in STL. But that doesn't work for me: #include <complex> #include <fftw3.h> int main() { std::complex<double> x(1,0); fftw_complex fx; fx = reinterpret_cast<fftw_complex>(x); } This gives me an error: error: invalid cast from type ‘std::complex<double>’ to type ‘double [2]’ What am I doing wrong? 回答1: Re-write your code as follows: #include <complex> #include <fftw3.h> int main() { std::complex

Can Aliasing Problems be Avoided with const Variables

百般思念 提交于 2019-11-30 09:43:32
My company uses a messaging server which gets a message into a const char* and then casts it to the message type. I've become concerned about this after asking this question . I'm not aware of any bad behavior in the messaging server. Is it possible that const variables do not incur aliasing problems? For example say that foo is defined in MessageServer in one of these ways: As a parameter: void MessageServer(const char* foo) Or as const variable at the top of MessageServer : const char* foo = PopMessage(); Now MessageServer is a huge function, but it never assigns anything to foo , however at

Is it safe to reinterpret_cast an enum class variable to a reference of the underlying type?

左心房为你撑大大i 提交于 2019-11-30 08:49:47
I've seen reinterpret_cast used to apply incrementation to enum classes, and I'd like to know if this usage is acceptable in standard C++. enum class Foo : int8_t { Bar1, Bar2, Bar3, Bar4, First = Bar1, Last = Bar4 }; for (Foo foo = Foo::First; foo <= Foo::Last; ++reinterpret_cast<int8_t &>(foo)) { ... } I know casting to a reference of a base class is safe in case of trivial classes. But since enum classes are not event implicitly converted to their underlying types, I'm not sure if and how the code above would be guaranteed to work in all compilers. Any clues? You might want to overload