function templates are not being compiled !

  • Thread starter Thread starter Alfonso Morra
  • Start date Start date
A

Alfonso Morra

I have written a class, and several of it's methods are function
templates. When I compiled the code, I realized that I had made some
typos in the code - but the compiler seemed to skip right over these
obvious errors. I gre suspicious and enterred XXX in one of the function
templates - and the compiler succesfully compile the code (obviously
completely skipped my templated functions).

I am using VC 7.1 Is this a "bug", (highly unlikely, I should think) or
have I missed something obvious.?
 
Alfonso said:
I have written a class, and several of it's methods are function
templates. When I compiled the code, I realized that I had made some
typos in the code - but the compiler seemed to skip right over these
obvious errors.

Templates don't generate code until they are instantiated. It is a
feature, and a very nice one, for that matter. It helps generic
programming, because you can define functions that may not apply to all
specializations. For example:

template <class Container>
bool IsMember(const Container& items, const Container::referece_type value)
{
return items.find(value) != items.end();
}

This function is always there, but it can only be called if the
container has a .find() member function, or else it will fail at compile
time. On the other hand, as long as the function is not called, it won't
issue any errors.

Note that the compiler doesn't know anything about Container until it
actually sees a specific instantiation. There's nothing in the above
template code that advises that Container has a .find method that takes
a single input, and an .end() method that takes no args, nor is it clear
that the two are comparable via operator!=. Only when you actually
create an instance of a template class or call a template function will
the compiler do a full syntax check and validation.

Tom
 
Tamas Demjen said:
Alfonso said:
I have written a class, and several of it's methods are function
templates. When I compiled the code, I realized that I had made some
typos in the code - but the compiler seemed to skip right over these
obvious errors.

Templates don't generate code until they are instantiated. [...]

Actually, it's not that simple. According to the
standard, compilers are required to do what's
called two-phase name lookup. This means that all
identifiers that do not depend on any template
arguments will be checked when the template
definition is seen by the compiler. Dependant
identifiers will only be checked when the
template is actually instanciated.
That said, VC unfortunately doesn't implement
two-phase name lookup. It just skips over any
template definitions that aren't instanciated.
Two me that's a real PITA, as my template code
needs to compile on a few compliant compilers
which often (and rightfully) bark at what VC
accepts.
To Alfonso: If you can, try to compile your
code with a compiler which implements two-phase
lookup in order to catch those errors. If you
can't, try to instanciate all templates in some
test app (although this might miss some errors).

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 
Alfonso said:
I have written a class, and several of it's methods are function
templates. When I compiled the code, I realized that I had made some
typos in the code - but the compiler seemed to skip right over these
obvious errors. I gre suspicious and enterred XXX in one of the function
templates - and the compiler succesfully compile the code (obviously
completely skipped my templated functions).

I am using VC 7.1 Is this a "bug", (highly unlikely, I should think) or
have I missed something obvious.?

It isn't a bug, though it is a slight annoyance. The C++ standard leaves
it up to the implementation as to when syntax errors in templates are
diagnosed. Many compilers don't perform any syntax checking at all until
a template is instantiated. Others perform every bit of syntax checking
that is possible prior to template instantiation (e.g. non-dependent
names are looked up, etc.) and then do the remaining at instantiation
time. VC++ belongs to the former group, Comeau C++ and other EDG based
compilers (such as Intel C++) to the latter group, and I think that GCC
lies somewhere in the middle - certainly it diagnoses the error in this
program, unlike VC7.1:

template <class T>
void f()
{
g();
}

int main()
{
}

That code is ill-formed, since there is no g() visible, but it is up to
the compiler whether it diagnoses the error or not.

Tom
 
Hendrik said:
<snip>
That said, VC unfortunately doesn't implement
two-phase name lookup. It just skips over any
template definitions that aren't instanciated.
</snip>

<rant>
That's just what I'd expect from a M$ compiler. I really wouldn't touch
it with a barge pole if I didn't have to.
</rant end>

Thanks for the tip.
 
template said:
void f()
{
g();
}

int main()
{
}

That code is ill-formed, since there is no g() visible, but it is up to
the compiler whether it diagnoses the error or not.

Tom,

"g()" might be a call to a "void g(T t = T());" and therefore it's template
dependant.
 
Vladimir said:
Tom,

"g()" might be a call to a "void g(T t = T());" and therefore it's template
dependant.

No, it might not, since default arguments are never used as part of
template argument deduction, and in any case, the use of "g" does not
match any uses of a name that make it a dependent-name in the C++
standard. It might indeed be an attempted call to void g(int i = 4),
double g(std::string foo = "Hello") or int g(), but since there is no
such function declared before the definition of f(), any such definition
won't be found by name lookup, and therefore the code is ill-formed.

Another similar example:

template <class T>
void f()
{
g();
}

void g()
{
}

int main()
{
f<int>();
}

Unfortunately, VC7.1 compiles that successfully, even though it is
ill-formed, and does require a diagnostic. This is because VC7.1 doesn't
have non-dependent name lookup implemented. That code fails to compile
on other popular, more standards compliant compilers, like GCC 3.4+ and
Intel C++.

Tom
 
template said:
No, it might not, since default arguments are never used as part of
template argument deduction, ...

OK, you are right :-)
I've checked the spec: 14.6.2 "... In an expression of the form:
postfix-expression ( expression-listopt )
where the postfix-expression is an identifier, the identifier denotes a
dependent name if and only if any of the expressions in the expression-list
is a type-dependent expression ..."
 
Back
Top