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

Why is this value downcast (static_cast to object type) allowed in C++20?

To my surprise, gcc 11.2 accepts this code, but only in C++20 mode: struct Base {}; struct Derived : Base { int i; }; int f(Base& b) { return static_cast(b).i; } // ^~~~~~~ oops, forgot the `&` Likewise,…
ecatmur
  • 152,476
  • 27
  • 293
  • 366
22
votes
5 answers

C++: can't static_cast from double* to int*

When I try to use a static_cast to cast a double* to an int*, I get the following error: invalid static_cast from type ‘double*’ to type ‘int*’ Here is the code: #include int main() { double* p = new double(2); int* r; …
samoz
  • 56,849
  • 55
  • 141
  • 195
21
votes
2 answers

C++ difference between adding const-ness with static_cast and const_cast of "this" object?

As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast(*this).Methodology(); however, in…
Casey
  • 10,297
  • 11
  • 59
  • 88
19
votes
2 answers

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

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(static_cast(pValues)); The…
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
17
votes
2 answers

Is it legal to static_cast a string_view to a string

My question is motivated by this answer on stackoverflow, https://stackoverflow.com/a/48082010/5360439. To quote, Q: How you convert a std::string_view to a const char*? A: Simply do a std::string(string_view_object).c_str() to get a guaranteed…
MaxPlankton
  • 1,158
  • 14
  • 28
16
votes
4 answers

What is the difference between static_cast and Implicit_cast?

What is implicit_cast? when should I prefer implicit_cast rather than static_cast?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
16
votes
1 answer

Will int to double conversion round up, down or to nearest double?

Simple question: Will the conversion from an int, say 100, to a double "round" up or down to the next double or will it always round to the nearest one (smallest delta)? e.g. for static_cast(100): Which way will it cast if d2 < d1? Bonus…
glades
  • 3,778
  • 1
  • 12
  • 34
15
votes
6 answers

When is static cast safe when you are using multiple inheritance?

I found myself in a situation where I know what type something is. The Type is one of three (or more) levels of inheritance. I call factory which returns B* however T is either the highest level of a type (if my code knows what it is) or the 2nd…
user34537
15
votes
1 answer

How to static cast throwing function pointer to noexcept in C++17?

C++17 makes noexcept part of a function's type. It also allows implicit conversions from noexcept function pointers to potentially throwing function pointers. void (*ptr_to_noexcept)() noexcept = nullptr; void (*ptr_to_throwing)() = ptr_to_noexcept;…
Filipp
  • 1,843
  • 12
  • 26
15
votes
1 answer

Is static_cast misused?

I have mixed feelings about static_cast, as it is the safest C++ cast available, but allows both safe and unsafe conversions at the same time, so you have to know the context to say if it is actually safe or might lead to UB (e.g. when casting to a…
Roman L
  • 3,006
  • 25
  • 37
15
votes
2 answers

Call of overloaded static_cast is ambiguous

I have some code like this struct B { B() {} B(int v) {} }; struct A { operator int() const { return 1; } operator B() const { return B(); } }; int main() { A a; static_cast(a); // Error here a.operator B(); // This…
Ivan Romanov
  • 1,138
  • 1
  • 9
  • 24
15
votes
1 answer

Why does std::forward return static_cast and not static_cast?

Let's have a function called Y that overloads: void Y(int& lvalue) { cout << "lvalue!" << endl; } void Y(int&& rvalue) { cout << "rvalue!" << endl; } Now, let's define a template function that acts like std::forward template void f(T&&…
gedamial
  • 1,498
  • 1
  • 15
  • 30
15
votes
2 answers

Can static_cast to same type introduce runtime overhead?

I have a structure template that takes two types (T and S), and at some point uses a static_cast to convert from one type to the other. It is often the case that T and S are the same type. A simplified example of the setup: template
marack
  • 2,024
  • 22
  • 31
14
votes
1 answer

What's up with static_cast with multiple arguments?

Can anyone tell me what this cast has for effect (besides setting happyNumber to 1337), if any at all, and if it has no other effect, how come I can write code like this??? Is this a compiler bug, or some "hidden away feature" of C++? int…
Statement
  • 3,888
  • 3
  • 36
  • 45
14
votes
2 answers

static_cast safety

AFAIK, for pointers/references static_cast, if a class definition is not visible to compiler at this point, then static_cast will be behave like reinterpret_cast. Why is static_cast unsafe for pointers/references and is safe for numeric values?
dimba
  • 26,717
  • 34
  • 141
  • 196
1
2
3
33 34