Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

function templates are not being compiled !

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alfonso Morra - 20 Jul 2005 21:22 GMT
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.?
Tamas Demjen - 20 Jul 2005 23:39 GMT
> 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
Hendrik Schober - 21 Jul 2005 11:08 GMT
> > 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).

> Tom

 Schobi

Signature

SpamTrap@gmx.de 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 Morra - 21 Jul 2005 12:44 GMT
><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.
Tom Widmer [VC++ MVP] - 21 Jul 2005 11:13 GMT
> 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
[quoted text clipped - 5 lines]
> 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
Vladimir Nesterovsky - 21 Jul 2005 12:21 GMT
> template <class T>
> void f()
[quoted text clipped - 8 lines]
> 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.
Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: www.nesterovsky-bros.com

Tom Widmer [VC++ MVP] - 21 Jul 2005 14:09 GMT
>>template <class T>
>>void f()
[quoted text clipped - 13 lines]
> "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
Vladimir Nesterovsky - 21 Jul 2005 14:52 GMT
>>>template <class T>
>>>void f()
[quoted text clipped - 11 lines]
> 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 ..."
Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com 


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.