static-cast

Casting double array to a struct of doubles

自闭症网瘾萝莉.ら 提交于 2019-11-28 01:51:36
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 its initial member (...) and vice versa. if I replace my code with struct A { double & x() { return data[0]; }

What is the difference between static_cast and Implicit_cast?

ε祈祈猫儿з 提交于 2019-11-27 19:51:24
What is implicit_cast? when should I prefer implicit_cast rather than static_cast? Johannes Schaub - litb I'm copying over from a comment i made to answer this comment at another place. You can down-cast with static_cast . Not so with implicit_cast . static_cast basically allows you to do any implicit conversion, and in addition the reverse of any implicit conversion (up to some limits. you can't downcast if there is a virtual base-class involved). But implicit_cast will only accept implicit conversions. no down-cast, no void*->T* , no U->T if T has only explicit constructors for U. Note that

C++ When should we prefer to use a two chained static_cast over reinterpret_cast

别说谁变了你拦得住时间么 提交于 2019-11-27 19:06:56
问题 First of all, this is not a duplicate of Why do we have reinterpret_cast in C++ when two chained static_cast can do it's job?. I know situations where we cannot use even two chained static_cast to achieve that, what reinterpret_cast does. But is there any situation where I should prefer a two chained static_cast over a simple and more readable reinterpret_cast ? 回答1: reinterpret_cast should be a huge flashing symbol that says THIS LOOKS CRAZY BUT I KNOW WHAT I'M DOING. Don't use it just out

C++ cannot convert from base A to derived type B via virtual base A

笑着哭i 提交于 2019-11-27 17:57:46
I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual base A In order to understand the cast system you need to dive in the object model. The classic representation of a simple hierarchy model is containment: that if B derives from A then the B object will in fact contain a A subobject alongside its own attributes. With this model, downcasting is a simple pointer manipulation, by an offset known at

C++ When should we prefer to use a two chained static_cast over reinterpret_cast

别等时光非礼了梦想. 提交于 2019-11-27 16:06:31
First of all, this is not a duplicate of Why do we have reinterpret_cast in C++ when two chained static_cast can do it's job? . I know situations where we cannot use even two chained static_cast to achieve that, what reinterpret_cast does. But is there any situation where I should prefer a two chained static_cast over a simple and more readable reinterpret_cast ? reinterpret_cast should be a huge flashing symbol that says THIS LOOKS CRAZY BUT I KNOW WHAT I'M DOING. Don't use it just out of laziness. reinterpret_cast means "treat these bits as ..." Chained static casts are not the same because

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

纵然是瞬间 提交于 2019-11-27 06:53:07
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! So why do we have reinterpret_cast in C++ when two chained static_cast can do its job? Some of you

Is it legal to cast a pointer to array reference using static_cast in C++?

送分小仙女□ 提交于 2019-11-27 06:37:54
问题 I have a pointer T * pValues that I would like to view as a T (&values)[N] In this SO answer https://stackoverflow.com/a/2634994/239916, the proposed way of doing this is T (&values)[N] = *static_cast<T(*)[N]>(static_cast<void*>(pValues)); The concern I have about this is. In his example, pValues is initialized in the following way T theValues[N]; T * pValues = theValues; My question is whether the cast construct is legal if pValues comes from any of the following constructs: 1: T theValues[N

What does static_cast<T> do to a T&?

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:08:13
问题 So I asked this question and I was tinkering around with solving it via static_cast . (Incidentally it does solve the problem, I'm just not sure if I understand why.) In the code: vector<int> foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo), static_cast<int>(foo.front()), 13); Is the static_cast simply constructing an R-Value int ? What's the difference between that and just the call: replace(begin(foo), end(foo), int{foo.front()}, 13); EDIT: As inferred by the answers static_cast

C++ cannot convert from base A to derived type B via virtual base A

笑着哭i 提交于 2019-11-27 04:14:19
问题 I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual base A 回答1: In order to understand the cast system, you need to dive into the object model. The classic representation of a simple hierarchy model is containment: if B derives from A then the B object will, in fact, contain an A subobject alongside

Why can't I static_cast between char * and unsigned char *?

风格不统一 提交于 2019-11-27 01:01:04
问题 Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule? 回答1: They are completely different types see standard: 3.9.1 Fundamental types [basic.fundamental] 1 Objects declared as characters char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single