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
8
votes
3 answers

Why is static_cast of void* to another type allowed?

I was just reading this thread: Simple c++ pointer casting And that got me to thinking why a static_cast between different pointer types is not allowed (except in the cases in which it is) unless you static_cast to a void* as an intermediary step.…
Dave Lillethun
  • 2,978
  • 3
  • 20
  • 24
7
votes
2 answers

Conversion operator implemented with static_cast

I ask this question following the issue I raised here. The point is quite simple. Suppose you have two classes of this kind: template < class Derived > class Base { ... operator const Derived&() const { return static_cast< const Derived&…
Massimiliano
  • 7,842
  • 2
  • 47
  • 62
7
votes
3 answers

ambiguous call to overloaded function

I have two functions: void DoSomething( const tchar* apsValue ) void DoSomething( size_t aiValue ) Now I want to pass '0' as a size_t: DoSomething(0); The compiler throws an error: "ambiguous call to overloaded function" To solve this, I can use…
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
7
votes
2 answers

old-style simple cast precedence in c++

I have some old-style casting in some c++ code which I would like to convert to new-style. I had a look to precedence and associativity operators documentation, but I failed to understand it. ( double ) myValueA() / myValueB() is equivalent to…
Denis Rouzaud
  • 2,412
  • 2
  • 26
  • 45
7
votes
2 answers

Why is a cast operator to std::optional ignored?

This code #include #include struct foo { explicit operator std::optional() { return std::optional( 1 ); } explicit operator int() { return 2; } }; int main() { foo my_foo; …
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
7
votes
2 answers

What does static_cast do to a T&?

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 foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo),…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
7
votes
4 answers

Can static_cast turn a non-null pointer into a null pointer?

I need to write code for a callback function (it will be called from within ATL, but that's not really important): HRESULT callback( void* myObjectVoid ) { if( myObjectVoid == 0 ) { return E_POINTER; } CMyClass* myObject =…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
7
votes
1 answer

reinterpret_cast / static_cast and undefined behavior

In a variant class I'm working on the raw storage is a char array: alignas(/* the strictest alignment of all types of the variant */) char storage[/* ... */]; The assignment operator is something like: template void operator=(const X…
manlio
  • 18,345
  • 14
  • 76
  • 126
7
votes
2 answers

Why is static_cast on an expression acting distributively?

I need to take 2 unsigned 8-bit values and subtract them, then add this value to a 32-bit accumulator. The 8-bit subtraction may underflow, and that's ok (unsigned int underflow is defined behavior, so no problems there). I would expect that…
Joel Geddert
  • 195
  • 6
7
votes
9 answers

Is my method for avoiding dynamic_cast<> faster than dynamic_cast<> itself?

I was answering a question a few minutes ago and it raised to me another one: In one of my projects, I do some network message parsing. The messages are in the form of: [1 byte message type][2 bytes payload length][x bytes payload] The format and…
ereOn
  • 53,676
  • 39
  • 161
  • 238
7
votes
3 answers

What wording in the C++ standard allows static_cast(malloc(N)); to work?

As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void*-to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a…
ben
  • 1,994
  • 12
  • 20
7
votes
5 answers

Accessing subclass members from a baseclass pointer C++

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each…
David Mason
  • 2,917
  • 4
  • 30
  • 45
7
votes
4 answers

Can I cast an unsigned char* to an unsigned int*?

error: invalid static_cast from type ‘unsigned char*’ to type ‘uint32_t* {aka unsigned int*}’ uint32_t *starti = static_cast(&memory[164]); I've allocated an array of chars, and I want to read 4 bytes as a 32bit int, but I get a…
David Mulder
  • 7,595
  • 11
  • 45
  • 61
7
votes
2 answers

Are there performance risks for using static_cast to deal with a vector of mixed (base & derived) objects? (aka "it this a dumb idea?")

Given a base class of gameObject and a derived class of animatedGameObject, I thought it may be good to store all of their instances in an std::vector. If the vector GameObjects is declared to be the base type of gameObject*, derived object…
ilzxc
  • 222
  • 1
  • 7
7
votes
2 answers

static_cast void* char* vs static_cast void** char**

If I do the following all is ok: char* cp = "abc"; void* vp = NULL; vp = static_cast(cp);//ok cp = static_cast(vp);//ok But the following is not: char** cpp = &cp; void** vpp = NULL; vpp = static_cast(cpp);//error C2440:…
e244
  • 71
  • 2