static-cast

C++ static_cast runtime overhead

旧城冷巷雨未停 提交于 2019-11-26 20:52:37
问题 See the code below. a) Does, in this case (simple inheritance, no virtual members), the static cast in B::df() have any overhead (whatsoever)? I found some conflicting answers to similar questions, that's why I am asking... b) I was thinking about making const M1 * func private in A and introducing a new private field const M2 * func into B to avoid the cast, but it kind of complicates things up and makes use of smart pointers more difficult. Do you see a better way to avoid the cast? class

Why do we have reinterpret_cast in C++ when two chained static_cast can do its job?

大城市里の小女人 提交于 2019-11-26 12:57:55
问题 Say I want to cast A* to char* and vice-versa, we have two choices (I mean, many of us think we\'ve two choices, because both seems to work! Hence the confusion!): struct A { int age; char name[128]; }; A a; char *buffer = static_cast<char*>(static_cast<void*>(&a)); //choice 1 char *buffer = reinterpret_cast<char*>(&a); //choice 2 Both work fine. //convert back A *pA = static_cast<A*>(static_cast<void*>(buffer)); //choice 1 A *pA = reinterpret_cast<A*>(buffer); //choice 2 Even this works fine

Casting double array to a struct of doubles

余生长醉 提交于 2019-11-26 11:37:57
问题 Is it OK to cast a double array to a struct made of doubles? struct A { double x; double y; double z; }; int main (int argc , char ** argv) { double arr[3] = {1.0,2.0,3.0}; A* a = static_cast<A*>(static_cast<void*>(arr)); std::cout << a->x << \" \" << a->y << \" \" << a->z << \"\\n\"; } This prints 1 2 3 . But is it guaranteed to work every time with any compiler? EDIT: According to 9.2.21: A pointer to a standard-layout struct object, suitably converted ? using a reinterpret_cast, points to

Why can&#39;t static_cast be used to down-cast when virtual inheritance is involved?

蹲街弑〆低调 提交于 2019-11-26 09:36:34
问题 Consider the following code: struct Base {}; struct Derived : public virtual Base {}; void f() { Base* b = new Derived; Derived* d = static_cast<Derived*>(b); } This is prohibited by the standard ( [n3290: 5.2.9/2] ) so the code does not compile, because Derived virtually inherits from Base . Removing the virtual from the inheritance makes the code valid. What\'s the technical reason for this rule to exist? 回答1: The technical problem is that there's no way to work out from a Base* what the

What is the difference between static_cast<> and C style casting?

橙三吉。 提交于 2019-11-26 00:40:38
问题 Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is their any sort of speed difference? 回答1: C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime also, c++ style casts can be searched for easily, whereas it's really hard to search for c style casts Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly. When writing C++ I'd pretty much always use the C++ ones over

Should I use static_cast or reinterpret_cast when casting a void* to whatever

陌路散爱 提交于 2019-11-26 00:32:42
问题 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? 回答1: Use static_cast : it is the narrowest cast that exactly describes what conversion is made here. There’s a misconception that using reinterpret_cast would be a better match because it means “completely ignore type safety and just cast from A to B”. However, this doesn’t actually describe the effect of a reinterpret_cast . Rather,