473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

function templates are not being compiled !

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.?

Nov 17 '05 #1
7 1135
Alfonso Morra wrote:
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
Nov 17 '05 #2
Tamas Demjen <td*****@yahoo.com> wrote:
Alfonso Morra wrote:
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

--
Sp******@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
Nov 17 '05 #3
Alfonso Morra wrote:
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
Nov 17 '05 #4
> 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,

"g()" might be a call to a "void g(T t = T());" and therefore it's template
dependant.
--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: www.nesterovsky-bros.com

Nov 17 '05 #5


Hendrik Schober wrote:
<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.

Nov 17 '05 #6
Vladimir Nesterovsky wrote:
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,

"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
Nov 17 '05 #7
>>>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.


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 ..."
--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
0
by: JimmyS | last post by:
I am getting this error .. c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\stl_alloc.h(305) : fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 2701)...
2
by: Simon White | last post by:
Hi, I have code that looks legal to me and has compiled for years and on latest VC, etc compilers. Just recently gcc made a change to their compiler (3.4) that broke it. I don't have access to...
1
by: Arne Petersen | last post by:
Hy, I've got a problem with member function templates compiled into libraries. I'm trying to get a library collection (coded for GNU gcc, where its compiled completly) being compiled on Visual...
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
3
by: Scythen | last post by:
I thought that including template headers such as <vector> in the pch is not useful. Templates can’t be compiled without the template arguments so there is no way to pre compile them. Is this...
5
by: Sandra-24 | last post by:
Is there a way in python to add the items of a dictionary to the local function scope? i.e. var_foo = dict. I don't know how many items are in this dictionary, or what they are until runtime. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.