reinterpret-cast

Need clarifications in C-style, reinterpret, and const casts

微笑、不失礼 提交于 2019-12-22 05:29:30
问题 Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly

Need clarifications in C-style, reinterpret, and const casts

落花浮王杯 提交于 2019-12-22 05:28:59
问题 Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly

How does an unspecified pointer conversion behave in C++14?

跟風遠走 提交于 2019-12-21 13:05:19
问题 The result of some pointer casts are described as unspecified. For example, [expr.static.cast]/13: A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” [...] If the original pointer value represents the address A of a byte in memory and A satisfies the alignment requirement of T, then the resulting pointer value represents the same address as the original pointer value, that is, A. The result of any other such pointer conversion is unspecified . My

how to use reinterpret_cast to cast to a derived class pointer in c++

こ雲淡風輕ζ 提交于 2019-12-21 12:34:28
问题 Here is my test example: struct base { virtual ~base(){} int x; }; struct derived: public virtual base { base * clone() { return new derived; } derived(): s("a") {} std::string s; }; int main () { derived d; base * b = d.clone(); derived * t = reinterpret_cast<derived*>(b); std::cout << t->s << std::endl; return 0; } It crashes at the line where I print s. Since "b" is a pointer to the derived class, reinterpret_cast should just work. I wonder why it crashes. At the same time, if I replace

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

只愿长相守 提交于 2019-12-19 04:13:13
问题 This question already has answers here : Does this really break strict-aliasing rules? (2 answers) Closed 2 years ago . 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

reinterpret_cast, char*, and undefined behavior

☆樱花仙子☆ 提交于 2019-12-18 13:06:48
问题 What are the cases where reinterpret_cast ing a char* (or char[N] ) is undefined behavior, and when is it defined behavior? What is the rule of thumb I should be using to answer this question? As we learned from this question, the following is undefined behavior: alignas(int) char data[sizeof(int)]; int *myInt = new (data) int; // OK *myInt = 34; // OK int i = *reinterpret_cast<int*>(data); // <== UB! have to use std::launder But at what point can we do a reinterpret_cast on a char array and

reinterpret_cast cast cost

主宰稳场 提交于 2019-12-18 12:45:13
问题 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? 回答1: It's a good assumption to start with. However, the optimizer may be restricted in what it can assume in the presence of a reinterpret_cast<> or C pointer cast. Then, even though the cast itself has no associated instructions, the resulting code is slower. For instance, if you cast an int to a pointer, the optimizer likely will

Can reinterpret_cast (or any cast) convert xvalues to lvalues?

让人想犯罪 __ 提交于 2019-12-18 04:12:37
问题 Is the following code legal (by C++11 and/or C++14 standard(s))? #include <iostream> #include <utility> using namespace std; void foo(int &a) { cout << a << endl; } int main() { foo(reinterpret_cast<int &>(move(5))); } If yes, is it undefined behavior? If it's not undefined behavior, can I even mutate a inside foo without it becoming UB? It compiles on clang 3.5, not on gcc 4.9. GCC error: ➤ g++-4.9 -std=c++1y sample.cpp -o sample sample.cpp: In function 'int main()': sample.cpp:11:40: error:

Is it a strict aliasing violation to alias a struct as its first member?

十年热恋 提交于 2019-12-18 03:03:47
问题 Sample code: struct S { int x; }; int func() { S s{2}; return (int &)s; // Equivalent to *reinterpret_cast<int *>(&s) } I believe this is common and considered acceptable. The standard does guarantee that there is no initial padding in the struct. However this case is not listed in the strict aliasing rule (C++17 [basic.lval]/11): If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined: (11.1) the

reinterpret_cast

瘦欲@ 提交于 2019-12-17 19:17:58
问题 In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book, and in chapter (8), it mentions the following about reinterpret_cast ....converts from one pointer type (int ) to another (char*). Because the cast changes the way the data pointed to is interpreted, it is called reinterpret_cast, as opposed to static_cast.* Can you describe this paragraph here? Especially the reason for the way the operation is named? Thanks. 回答1: Basically, the reinterpret_cast reinterprets the bit