I don't understand why use StaticCast instead of static_cast?

Hi, I look inside StaticCast.

The comment said

“This exists to avoid a Visual Studio bug where using a cast to forward an rvalue reference array argument to a pointer parameter will cause bad code generation. Wrapping the cast in a function causes the correct code to be generated.”

The part which I don’t understand is “cast to forward an rvalue reference array argument to a pointer parameter”.

Is that means some kind like this?

void Func(SomeType* pointer) {}

void Func(SomeType&& arg) 
{
    Func(static_cast<SomeType*>(&arg));
}

SomeType arr[10];
Func(std::move(arr[1]));

Honestly I don’t understand at all.

what is “rvalue reference array argument” mean?

Please give me some sample code.

In C++, both StaticCast and static_cast are used to perform a static cast, which is a type of casting that allows you to convert a variable from one type to another at compile time. However, they are used in slightly different contexts.

static_cast is a C++ keyword that is used to perform a static cast. It can be used to convert between built-in data types (such as int to float), pointer types, and class-based types (such as a base class pointer to a derived class pointer). It is a standard C++ operator and is recognized by most C++ compilers.

On the other hand, StaticCast is a function template defined by the Unreal Engine that is used to perform a static cast. It is a wrapper around the C++ static_cast keyword and is used in Unreal Engine code to ensure that the static cast is being performed on the correct types. It also provides some additional error checking to ensure that the cast is safe and that the types being cast are compatible.

3 Likes

Thank you for your response!

But what I want to know is not the uses of StaticCast.

I want to know why use it and the reason is written in comment by Unreal.

“This exists to avoid a Visual Studio bug where using a cast to forward an rvalue reference array argument to a pointer parameter will cause bad code generation. Wrapping the cast in a function causes the correct code to be generated.”

I want to know the example of the comment.

StaticCast is not a macro.

Hard to say, beacuse we don’t know bug-id or if it still exists.
At first glance, if you don’t operate on C-style arrays (especially by passing it to template functions), you can safely use the built-in static_cast.

Oh thanks for the information. Is it more accurate to say that the StaticCast is a function template?

Yup. StaticCast

1 Like