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
14
votes
4 answers

c++ static_cast and references

struct A{}; struct B : A{}; int main() { A a; A& a_ref = a; static_cast(a); // *1 static_cast(a_ref); // *2 return 0; } (*1) produces an error and i understand why. (*2) compiles fine, but why? And, as long as it…
fogbit
  • 1,961
  • 6
  • 27
  • 41
13
votes
3 answers

reinterpret_cast error for enum

Why i can't use reinterpret_cast operator for such a cast? enum Foo { bar, baz }; void foo(Foo) { } int main() { // foo(0); // error: invalid conversion from 'int' to 'Foo' // foo(reinterpret_cast(0)); // error: invalid cast from type…
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
12
votes
3 answers

Why is this static_cast not allowed?

I have an object of class A that I want to allocate on a custom stack object. To do this, I simply move the stack pointer as many bytes as the object is in size and return its previous value: class A : public B {}; //B is from a precompiled…
NmdMystery
  • 2,778
  • 3
  • 32
  • 60
12
votes
4 answers

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast
DangerMouse
  • 704
  • 7
  • 20
11
votes
1 answer

Why is static_cast needed in the gcc's implementation of is_nothrow_constructible?

Taken from the GCC implementation of type_traits why is static_cast needed here? template struct __is_nt_constructible_impl : public integral_constant()...))> {}; template…
João Pires
  • 927
  • 1
  • 5
  • 16
11
votes
3 answers

How to implement the CRTP following MISRA C++

My team is developing a embedded system where we need to follow MISRA C++. We are refactoring the code to use less virtual methods so we are trying to implement the CRTP to use static polymorphism instead of the dynamic one. But we have the problem…
LeDYoM
  • 949
  • 1
  • 6
  • 21
11
votes
6 answers

Casting double array to a struct of doubles

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(static_cast(arr)); …
vitke
  • 111
  • 5
11
votes
1 answer

static_cast and reinterpret_cast for std::aligned_storage

could someone please explain the bit of code about casting in http://en.cppreference.com/w/cpp/types/aligned_storage please? can the following code return *static_cast(static_cast(&data[pos])); be replaced with return…
hong pei
  • 929
  • 2
  • 13
  • 27
10
votes
2 answers

How is the precision loss from integer to float defined in C++?

I've a question to the code snippet below: long l=9223372036854775807L; float f=static_cast(l); The long value cannot be represanted exactly according to the IEEE754. My Question is how is the lossy conversion handled: Is the nearest…
user1235183
  • 3,002
  • 1
  • 27
  • 66
10
votes
4 answers

Bizarre static_cast trick?

While perusing the Qt source code I came across this gem: template inline T qgraphicsitem_cast(const QGraphicsItem *item) { return int(static_cast(0)->Type) == int(QGraphicsItem::Type) || (item &&…
Rob
  • 76,700
  • 56
  • 158
  • 197
10
votes
1 answer

How can static_cast be used with virtual inheritance?

So it's impossible to downcast using static_cast with virtual inheritance, but how is it possible to do the following upcast: class Base {...}; class Derived : public virtual Base {...}; ... Derived *d = new Derived(); Base *b =…
user2628520
10
votes
2 answers

Why can't I use static_cast to pass an integer reference parameter to a function in C++?

I have an enum parameter in a C++ program that I need to obtain using a function that returns the value through a parameter. I started by declaring it as an int but at code review was asked to type it as the enum (ControlSource). I did this but it…
Gareth
  • 439
  • 5
  • 12
9
votes
1 answer

Why is it important to use static_cast instead of reinterpret_cast here?

At a reply of a blog post of Raymond Chen, A questioner pointed out Raymond, I believe the C++ example is not correct since the position of the base class subobject in the derived class is unspecified according to ISO C++ 2003 Standard (10-3,…
Benjamin
  • 10,085
  • 19
  • 80
  • 130
9
votes
0 answers

Why (void*)p instead of static_cast(p) in C++ standard?

ISO/IEC 14882:2020 22.2.1.16 Note 8: The default construct in allocator will call ::new ((void*)p) T(args), but specialized allocators can choose a different definition. Would you be so kind as to tell me whether there's any reason it's not ::new…
Ernie Sanderson
  • 382
  • 2
  • 10
9
votes
5 answers

How to implement a compile-time check that a downcast is valid in a CRTP?

I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template class Base { void MethodToOverride() { // generic stuff here } void…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
1 2
3
33 34