Templates and deferred instantiation

Started by
10 comments, last by Antheus 12 years, 2 months ago
In the OP's post, the template function Func() is defined on line 3. That function definition contains a call to Func2(). Because Func2() does not depend on T, it is a non-dependent name. As a non-dependent name it needs to be looked up at the point of use, which is line 3. However, at line 3 Func2() is not in scope, so should not be bound. The point is that look up should not be dependent on point of instantiation. Whatever statements and definitions appear between point of definition and point of instantiation, should not change the meaning of the non-dependent code of a template - at least according to the standard. In practice, this kind of insulation is hard for compiler vendors to implement correctly.
Advertisement
I missed that original template declaration has a body that calls Func2, somehow I just jumped to forward declaration-like assumption.

The comeau compiler has the following to say:

"ComeauTest.c", line 1: error: identifier "Func2" is undefined
template <typename T> T Func (T a) { return Func2(a); }
^
detected during instantiation of "T Func(T) [with T=int]" at line 6

"ComeauTest.c", line 1: error: identifier "Func2" is undefined
template <typename T> T Func (T a) { return Func2(a); }
^
detected during instantiation of "T Func(T) [with T=float]" at line 7

2 errors detected in the compilation of "ComeauTest.c".
[/quote]

Obviously, it's the expected error.

This topic is closed to new replies.

Advertisement