【发布时间】:2017-01-12 15:30:14
【问题描述】:
我正在尝试使用现代 C++ 'auto' 并发现一个产生错误的简单示例,但我不明白为什么:
main.cpp
// error: use of ‘auto test(int)’ before deduction of ‘auto’ int i = test(5);
int i = test(5);
test.h
auto test(int i);
test.cpp
auto test(int i) {
if (i == 1)
return i; // return type deduced as int
else
return Correct(i-1)+i; // ok to call it now
}
但是,如果我使用“->”指定类型,则代码构建并运行良好。例如:
auto test(int i) -> int;
g++ 6.2 是现代版本的编译器,我想知道为什么我必须使用'-> int'。感谢您的建议。
【问题讨论】: