【发布时间】:2016-04-18 13:44:43
【问题描述】:
我在 stackoverflow C++ 11 auto compile time or runtime? 中搜索并找到了解决方案。它是重复的,但不完全是,答案说有编译时间。真的是编译时间吗?让我们考虑下面的例子。
auto give_something(bool mybool)
{
if (mybool)
return string("auto");
return 6.66f;
}
int main()
{
bool mybool = (rand() % 2) ? true : false;
auto x = give_something(mybool); // how type of x is deduced?
return 0;
}
这里x的类型和give_something的返回类型编译时无法推断(我的假设)。它应该只在运行时推导出来。那么auto 是编译时还是运行时还是两者兼而有之?
【问题讨论】:
-
Does auto deduce types in compile time or run time in C++11?: 在编译时,除了少数例外,它遵循模板参数推导规则。 -
编译器在编译时必须知道所有类型。
标签: c++ c++11 compilation c++14