本文介绍了如果有副作用,可以优化功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在主线程上初始化一些静态数据.
I want to initialize some static data on the main thread.
int32_t GetFoo(ptime t)
{
static HugeBarData data;
return data.Baz(t);
}
int main()
{
GetFoo(); // Avoid data race on static field.
// But will it be optimized away as unnecessary?
// Spawn threads. Call 'GetFoo' on the threads.
}
如果编译器可能决定删除它,我怎么能迫使它呆在那里?
If the complier may decide to remove it, how can I force it to stay there?
推荐答案
C ++编译器可以优化的唯一副作用是不必要的构造函数调用,尤其是复制构造函数.
The only side-effecting functions that a C++ compiler can optimize away are unnecessary constructor calls, particularly copy constructors.
这篇关于如果有副作用,可以优化功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!