This code is from "C++ programming language" by Bjarne Stroustrup (C.13.8.3 Point of Instantiation Binding)
template <class T>
void f(T value)
{
g(value);
}
void g(int v);
void h()
{
extern g(double);
f(2);
}
And he mentions:
Here, the point of instantiation for f() is just before h(), so the g() called in f() is the global g(int) rather than the local g(double). The definition of ‘‘instantiation point’’ implies that a template parameter can never be bound to a local name or a class member.
void h()
{
struct X {}; // local structure
std::vector<X> v; // error: can't use local structure as template parameter
}
My questions are:
Why should the first code work?
g()
is declared later, and I really get an error with G++ 4.9.2 thatg
isn't declared at that point.extern g(double) - how this works? since return value doesn't matter in case of function overloading, then we can miss it in forward declarations?
the point of instantiation for f() is just before h() - why? isn't it logical that it'll get instantiated when
f(2)
is being called? Right where we call it, whenceg(double)
will be in scope already.The definition of ‘‘instantiation point’’ implies that a template parameter can never be bound to a local name or a class member - Has this changed in C++14? I'm getting error with C++(G++ 4.9.2), but don't get error with C++14(G++ 4.9.2).