【问题标题】:Do static casts take execution time? [duplicate]静态强制转换需要执行时间吗? [复制]
【发布时间】:2015-11-22 23:44:56
【问题描述】:

我知道 dynamic_cast 确实需要执行时间,甚至被认为很慢,但是 static_cast 也需要执行时间吗?这是一个示例代码

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;
}

printv() 是否会比 prints() 花费更长的时间,因为它包含一个演员表?

【问题讨论】:

标签: c++


【解决方案1】:

static_cast 在运行时可能需要一些时间。例如,如果您将int 转换为float,则需要工作。通常转换指针不需要任何运行时成本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多