reinterpret-cast

Is reinterpret_cast type punning actually undefined behavior?

北城以北 提交于 2020-06-10 04:38:10
问题 It appears to be widely-held that type punning via reinterpret_cast is somehow prohibited (properly: "undefined behavior", that is, "behavior for which this International Standard imposes no requirements", with an explicit note that implementations may define behavior) in C++. Am I incorrect in using the following reasoning to disagree, and if so, why ? [expr.reinterpret.cast]/11 states: A glvalue expression of type T1 can be cast to the type “reference to T2 ” if an expression of type

Why do I Have to reinterpret_cast Pointer Pointers?

不羁的心 提交于 2020-01-24 10:00:06
问题 So this static_cast code is totally legal: int n = 13; void* pn = static_cast<void*>(&n); void** ppn = &pn; Yet this has to be made into a reinterpret_cast to compile: int n = 13; int* foo = &n; void** bar = static_cast<void**>(&foo); If I don't change it I get the error: error C2440: static_cast : cannot convert from int ** to void ** note: Types pointed to are unrelated; conversion requires reinterpret_cast , C-style cast or function-style cast So I take it the issue is "the types are

Turning vector of shared_ptr into vector of shared_ptr to const

家住魔仙堡 提交于 2020-01-24 03:05:35
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal

Turning vector of shared_ptr into vector of shared_ptr to const

被刻印的时光 ゝ 提交于 2020-01-24 03:04:37
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal

Turning vector of shared_ptr into vector of shared_ptr to const

纵然是瞬间 提交于 2020-01-24 03:04:24
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal

Why can't I reinterpret_cast uint to int?

。_饼干妹妹 提交于 2020-01-21 02:39:13
问题 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 C2440: 'reinterpret_cast' : cannot convert from 'const uint64_t' to 'int64_t' 1> Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast Why doesn't it compile? both types have the same bit length, isn't it what reinterpret_cast is

reinterpret_cast-ing vector of one type to a vector of another type which is of the same type

落爺英雄遲暮 提交于 2020-01-17 15:14:05
问题 I'm not sure if the question here answers this question due to the weird wording, but: if i have: struct numpair { some_type_with_a_size_of_2 a,b; }; struct bignum { some_type_with_a_size_of_4 a; }; Can I reinterpret_cast a vector of bignums to a vector of numpairs? If not, are there other workarounds that don't require me to make a new vector and go through reinterpret casting each element? edit: on visual studio 2017 windows, which i am using, these two types are the same size. edit: I have

c++ reinterpret_cast, virtual, and templates ok?

这一生的挚爱 提交于 2020-01-15 08:32:27
问题 In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public BaseClass { }; Further assume factory classes for these two classes with a common, templated base class: template<typename T> class Factory { public: virtual T* create() = 0; }; class BaseClassFactory : public Factory<BaseClass> { public: virtual BaseClass* create() { return new BaseClass(&m_field); } private: SomeClass m_field; }; class ChildClassFactory : public Factory<ChildClass> { public: virtual

C++ reinterpret cast?

僤鯓⒐⒋嵵緔 提交于 2020-01-06 09:56:07
问题 I would like to cast one object of the class PointsList to another object Points3DList (and vice versa) where: template <class T> class PointsList { protected: std::vector <Point <T> *> points; //Only illustration, not possible with templaes }; and template <class T> class Points3DList { protected: std::vector <Point3D<T> *> points; //Only illustration, not possible with templaes }; Between Point and Point3D there is no relationship (inheritance nor composition)... template <class T> class

Does the following reinterpret_cast lead to undefined behavior?

霸气de小男生 提交于 2020-01-04 07:30:13
问题 Does the reinterpret_cast in the code below lead to undefined behavior? In case it does, is it possible to define rpd in a type-safe manner? class Base { public: virtual ~Base() = default; }; class Derived : public Base { }; int main(void) { Derived d; Base* pb = &d; Base*& rpb = pb; Derived*& rpd = reinterpret_cast<Derived*&>(rpb); return 0; } Sort of related to my previous recent question. Context behind this; I am experimenting with an adapter class that should allow vectors containing