Questions tagged [static-cast]

A C++ cast operator to convert from one type to another, using only information about the static type of the object being cast

From cppreference.com:

Converts between types using a combination of implicit and user-defined conversions. It has the form static_cast<new_type>(expression).

static_cast is the C++ cast operator used to convert from one type to another. It only uses information about the static type of the object being cast and not its dynamic type; i.e. using only information known at compile-time and not performing a run-time check. The conversion returns a value of type being converted to.

As with all cast expressions, the result is:

  • an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
  • an xvalue if new_type is an rvalue reference to object type;
  • a prvalue otherwise.
507 questions
803
votes
9 answers

Why use static_cast(x) instead of (int)x?

I've heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?
Tommy Herbert
  • 20,407
  • 14
  • 52
  • 57
283
votes
7 answers

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

Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is there any sort of speed difference?
dicroce
  • 45,396
  • 28
  • 101
  • 140
257
votes
9 answers

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

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?
Andy
  • 3,004
  • 2
  • 22
  • 12
74
votes
4 answers

static_cast with boost::shared_ptr?

What is the equivalent of a static_cast with boost::shared_ptr? In other words, how do I have to rewrite the following Base* b = new Derived(); Derived* d = static_cast(b); when using shared_ptr? boost::shared_ptr b(new…
Frank
  • 64,140
  • 93
  • 237
  • 324
73
votes
7 answers

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

I have four 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…
69
votes
3 answers

Proper way of casting pointer types

Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast? I used…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
64
votes
3 answers

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

Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule?
Nick
  • 2,821
  • 5
  • 30
  • 35
53
votes
6 answers

Why can't static_cast be used to down-cast when virtual inheritance is involved?

Consider the following code: struct Base {}; struct Derived : public virtual Base {}; void f() { Base* b = new Derived; Derived* d = static_cast(b); } This is prohibited by the standard ([n3290: 5.2.9/2]) so the code does not…
Eran
  • 21,632
  • 6
  • 56
  • 89
44
votes
7 answers

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

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 =…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
41
votes
1 answer

How does qobject_cast work?

I just found the following code in Qt and I'm a bit confused what's happening here. Especially as to what reinterpret_cast(0) does? template inline T qobject_cast(const QObject *object) { // this will cause a compilation error if T…
ronag
  • 49,529
  • 25
  • 126
  • 221
30
votes
4 answers

is there any difference between static cast to rvalue reference and std::move

The description for static cast says If new_type is an rvalue reference type, static_cast converts the value of expression to xvalue. This type of static_cast is used to implement move semantics in std::move.(since C++11) Does this confirm that…
Aditya Sihag
  • 5,057
  • 4
  • 32
  • 43
26
votes
1 answer

Why is (int&)0 ill-formed?

According to [expr.cast]/4, a C-style cast tries the following casts in order: const_cast static_cast static_cast followed by const_cast reinterpret_cast reinterpret_cast followed by const_cast The following cast is…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
25
votes
1 answer

C++ static_cast runtime overhead

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…
Petr
  • 1,128
  • 2
  • 14
  • 23
24
votes
1 answer

Why is `decltype(static_cast(...))` not always `T`?

For the following code, all but the last assertion passes: template constexpr void assert_static_cast_identity() { using T_cast = decltype(static_cast(std::declval())); static_assert(std::is_same_v); } int…
Eric
  • 95,302
  • 53
  • 242
  • 374
23
votes
2 answers

Downcast in a diamond hierarchy

Why static_cast cannot downcast from a virtual base ? struct A {}; struct B : public virtual A {}; struct C : public virtual A {}; struct D : public B, public C {}; int main() { D d; A& a = d; D* p = static_cast(&a); //error } g++ 4.5…
log0
  • 10,489
  • 4
  • 28
  • 62
1
2 3
33 34