C++ Static_Cast Runtime Overhead

C++ static_cast runtime overhead

static_cast<T>(e) is equivalent to creating an invented temporary variable v in the following way:

T v(e); //where T is an arbitrary type  and e is an arbitrary expression.

The runtime cost of a static_cast is exactly the cost of the above statement

What is the static_cast runtime overhead if adding constness while keeping the same type?

The runtime overhead is essentially a pointer copy, which may be optimised out altogether.

But in your case, I'd consider changing int m_i; to mutable std::atomic<int> m_i; and make increment constant in the base class too. It looks like a reference counter and my way allows you to (i) retain const-correctness and (ii) be thread safe. I'd also consider changing int to unsigned to avoid undefined behaviour if m_i gets too big.

Can static_cast to same type introduce runtime overhead?

Yes, it can.

Here is an example:

struct A {
A( A const& ) {
std::cout << "expensive copy\n";
}
};

template<typename T>
void noop( T const& ) {}
template <typename T, typename S = T>
void bar(T val)
{
noop(static_cast<S>(val));
}
template <typename T>
void bar2(T val)
{
noop(val);
}
int main() {
std::cout << "start\n";
A a;
std::cout << "bar2\n";
bar2(a); // one expensive copy
std::cout << "bar\n";
bar(a); // two expensive copies
std::cout << "done";
}

basically, a static_cast can induce a copy constructor to be called.

For some types (like int), a copy constructor is basically free, and the compiler can eliminate it.

For other types, it cannot. In this context, copy elision isn't legal either: if your copy constructor has side effects or the compiler cannot prove that it has no side effects (common if the copy constructor is non-trivial), it will be called.

Do static casts take execution time?

static_cast MAY take time at run-time. For example, if you convert int to float then work is required. Usually casting pointers does not require any run-time cost.

Performance hit from C++ style casts?

If the C++ style cast can be conceptualy replaced by a C-style cast there will be no overhead. If it can't, as in the case of dynamic_cast, for which there is no C equivalent, you have to pay the cost one way or another.

As an example, the following code:

int x;
float f = 123.456;

x = (int) f;
x = static_cast<int>(f);

generates identical code for both casts with VC++ - code is:

00401041   fld         dword ptr [ebp-8]
00401044 call __ftol (0040110c)
00401049 mov dword ptr [ebp-4],eax

The only C++ cast that can throw is dynamic_cast when casting to a reference. To avoid this, cast to a pointer, which will return 0 if the cast fails.

Performance hit of static_cast pointer types?

Nitpicker's corner

Other answers pointed out that the cost is zero for pointers; this is correct as far as class hierarchies with single inheritance are involved, but when dealing with multiple inheritance there may be a (very small) cost.

In single-inheritance classes the memory layout of objects is typically arranged so that a pointer to a derived class can be used as a pointer to base class with no adjustments - this is normally accomplished by putting the "parent part" of the object at the front, so that a pointer to Derived is actually also a valid pointer to Base.

But in multiple-inheritance scenarios, you cannot put all the base classes in front of the object; for this reason, when casting a Derived * to Base1 * or to Base2 *, a pointer adjustment may be needed. This typically results in a fixed-size increment to the pointer when doing various operations that involve a pointer to one of the base classes.

Of course, this is completely negligible (it amounts to a single really fast assembly instruction at most), but nonetheless, in this scenario there is a very very slight cost in cast. Notice though that it's not specific to static_cast, it's there whenever you need to treat your Derived * as a Base2 * (including when you simply call Base2 * methods, since also the this pointer needs to be adjusted).

Additional reading: http://www.codeproject.com/Articles/12935/What-static_cast-is-actually-doing



Related Topics



Leave a reply



Submit