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
9
votes
1 answer

Why do compilers behave differently when static_cast(ing) a function to void*?

The following code compiles without any error in VSC++2017 and doesn't compile in gcc 7.3.0 (error: invalid static_cast from type ‘int(int)’ to type ‘void*’ void* p = static_cast(func)) #include int func(int x) { return 2 * x;…
Soulimane Mammar
  • 1,658
  • 12
  • 20
9
votes
2 answers

static_cast'd pointer value

In the current draft standard (and C++17), this is written about static_casting a void *: A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same…
geza
  • 28,403
  • 6
  • 61
  • 135
9
votes
2 answers

reinterpret_cast for almost pod data (is layout-compatibility enough)

I am trying to learn about static_cast and reinterpret_cast. If I am correct the standard (9.2.18) says that reinterpret_cast for pod data is safe: A pointer to a POD-struct object, suitably converted using a reinterpret_cast, points to its …
Tom
  • 5,219
  • 2
  • 29
  • 45
9
votes
2 answers

Has CRTP no compile time check?

I was trying to implement static polymorphism using the Curiously Recurring Template Pattern, when I noticed that static_cast<>, which usually checks at compile time if a type is actually convertible to another, missed a typo in the base class…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
9
votes
1 answer

casting to the same type

I have this case: using T = classA; //T could be classA and could be `classB` in other platforms. T a; auto x = static_cast(a); In case that T is classA the casting is must. In case of T is classB the casting is redundant. By standard,…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
9
votes
2 answers

Replacing delete in C++, missinformation

I'm trying to (and have solved) a problem with 16 byte alignment issues with a class that contains SSE optimised members. But what is bugging me is a large portion of the examples I have found online contain a line of code that to me seems totally…
8
votes
5 answers

interpret unsigned as signed

I'm working on an embedded platform (ARM) and have to be careful when dealing with bit patterns. Let's pretend this line is beyond my influence: uint8_t foo = 0xCE; // 0b11001110 Interpreted as unsigned this would be 206. But actually it's…
Sven-de
  • 83
  • 1
  • 3
8
votes
2 answers

May I have a real life example where casting through void* works and reinterpret_cast doesn't?

There's a set of questions regarding cross-casts (cast from T1* to unrelated T2*), for example this and this. The answer usually goes like this: reinterpret_cast is implementation defined and conversion to void* followed by static_cast is…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
8
votes
6 answers

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

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…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
8
votes
2 answers

Why we are using static_cast to NULL

When I try to study QP/CPP code I came across below line. QTimeEvt *t; // ... if (t == static_cast(0)) { Why they are doing static_cast of 0? If they want to check NULL we can do that directly right? This source code you can find out…
Gilson PJ
  • 3,443
  • 3
  • 32
  • 53
8
votes
0 answers

Why does the compiler force this conversion below into a bool value?

I'm testing [expr.static.cast]/2 with the following snippet (see live example): #include struct B{ int i = 2; }; struct D: public B{}; int main() { D d; std::cout << &d << '\n'; std::cout << &(static_cast((B&)d)) << '\n'; …
Alexander
  • 2,581
  • 11
  • 17
8
votes
1 answer

static_cast Conversion constructor vs Conversion operator

After reading this I tried making such conversion with static_cast: class A; class B { public: B(){} B(const A&) //conversion constructor { cout << "called B's conversion constructor" << endl; …
PcAF
  • 1,975
  • 12
  • 20
8
votes
4 answers

Virtual functions and cast to void and back

Currently I am working with a legacy c++ code base. In this codebase pointer to objects are converted to void-pointers and then stored in a c-library. Consider the following code: class interface { public: virtual void foo() { std::cout <<…
IcePic
  • 293
  • 1
  • 10
8
votes
3 answers

Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast

So if your converting from Void* to Type* or from Type* to Void* should you use: void func(void *p) { Params *params = static_cast(p); } or void func(void *p) { Params *params = reinterpret_cast(p); } To me static_cast…
Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85
8
votes
3 answers

static_cast from Derived* to void* to Base*

I would like to cast a pointer to a member of a derived class to void* and from there to a pointer of the base class, like in the example below: #include class Base { public: void function1(){std::cout<<"1"<
Roberto
  • 3,003
  • 1
  • 18
  • 25