Consider this C++1y code (LIVE EXAMPLE):
#include <iostream>
auto foo();
int main() {
std::cout << foo(); // ERROR!
}
auto foo() {
return 1234;
}
The compiler (GCC 4.8.1) generously shoots out this error:
main.cpp: In function ‘int main()’:
main.cpp:8:18: error: use of ‘auto foo()’ before deduction of ‘auto’
std::cout << foo();
^
How do I forward-declare foo()
here? Or maybe more appropriately, is it possible to forward-declare foo()
?
I've also tried compiling code where I tried to declare foo()
in the .h
file, defined foo()
just like the one above in a .cpp
file, included the .h
in my main.cpp
file containing int main()
and the call to foo()
, and built them.
The same error occurred.
Best Answer
According to the paper it was proposed in, N3638, it is explicitly valid to do so.
Relevant snippet:
However it goes on to say:
So the fact that you used it before it was defined causes it to error.