473,809 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1154
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::refe rece_type value)
{
return items.find(valu e) != 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******@nester ovsky-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******@nester ovsky-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
4591
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 function (double, float etc) ?? Thanks in advance,
0
1722
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) when including any files for templates .. #include <fstream> , #include <string>, #include <vector>, #include <list> etc... The funny thing is that was not happening before, it really used to work fine with templates. By removing the STL include...
2
1699
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 the C++ standard but information we do have access to uptill now suggests that this shouldn't be a problem. A code example is this: template<class T> class Base
1
1749
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 Studio .NET. The problem is that for gcc the template functions (members of a class) are explicitly instantiated in a cpp file that includes the classes .h and .cpp file. The VS compiler does not correctly bring together the function template...
5
5307
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 adsq::QInitialise<struct adsq::Data>(struct adsq::Head<struct adsq::Data> *)" -----------adsq.h file
18
8931
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 to create multiple functions which they look almost identical in C++ source code. The C++ compiler should be able to compile one Test() function into two almost identical functions before they are translated into the machine language object. ...
16
16296
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
1535
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 true? Also, it seems that this could actually be bad since it will significantly increase the size of the pch file which I would think could slow the compilation processes due to the additional memory overhead.
5
3213
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. exec statements are difficult for debuggers to deal with, so as a workaround I built my code into a function and saved it in a .py file. The I load the .py file as a module and call the function instead. This works great, and it has the added...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.