1

I know that dynamic_cast does take execution time and even considered slow, but does static_cast take execution time as well? Here's an example code

void printv(const void *str) {
    std::cout << *static_cast<const std::string*>(str) << "\n";
}

void prints(const std::string *str) {
    std::cout << *str << "\n";
}

int main()
{
    std::string str("my string");

    printv(&str);
    prints(&str);

    system("pause");
    return 0;
}

Will printv() take longer than prints() because it includes a cast inside?

McLovin
  • 3,295
  • 7
  • 32
  • 67
  • http://stackoverflow.com/questions/6445841/c-static-cast-runtime-overhead- This may be better suitable to considered as a duplicate – Steephen Nov 22 '15 at 16:32

1 Answers1

4

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.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91