473,324 Members | 2,239 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,324 software developers and data experts.

A not very well publicized fact about C++ templates

For a template class, all the inline functions are not checked by the
compiler for syntax errors until and unless those inline functions are
explicitly called by the user in the code. Don't believe me? Try it
out yourself ;-)

Of course we all know that for a non template class, all the inline
functions are compiled and any encountered errors are reported during
compilation steps.

Looking back it does seem reasonable behavior, although I was
extremely surprised when I first saw it.

Regards,
KP Bhat
Jul 22 '05 #1
7 1543
On 20 Dec 2003 16:56:14 -0800, us****@sta.samsung.com (Generic Usenet Account) wrote:
For a template class, all the inline functions are not checked by the
compiler for syntax errors
That is incorrect. The syntax is checked. Possibly you're confusing
"syntax" with higher level meaning.

until and unless those inline functions are explicitly called by
the user in the code.
At instantiation the compiler must check whether the template function
is meaningful, e.g., if it uses foo.bar, then foo.bar must exist, have
a type compatible with the usage, and so forth.
Don't believe me? Try it out yourself ;-)
Don't confuse the behavior of a given compiler with the rules of the
language.
Of course we all know that for a non template class, all the inline
functions are compiled and any encountered errors are reported during
compilation steps.

Looking back it does seem reasonable behavior, although I was
extremely surprised when I first saw it.


I recommend Andrei Alexandrescu's book "Modern C++ Design" where he
makes good use of the delayed checking -- as well as other things.

Jul 22 '05 #2
"Generic Usenet Account" <us****@sta.samsung.com> wrote in message
news:90*************************@posting.google.co m
For a template class, all the inline functions are not checked by the
compiler for syntax errors until and unless those inline functions are
explicitly called by the user in the code. Don't believe me? Try it
out yourself ;-)

This is not true. This depends partly on the compiler, but the basic rule is
that the compiler checks what syntax it can, but doesn't check syntax that
depends for its validity on the particular template arguments chosen. See
Stroustrup, TC++PL, pp. 333-4.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3
Generic Usenet Account wrote in
news:90*************************@posting.google.co m:
For a template class, all the inline functions are not checked by the
compiler for syntax errors until and unless those inline functions are
explicitly called by the user in the code.
Only if your compiler doesen't support 2-phase name lookup, you need
to get a more up to date compiler or maybe give your up to date compiler
a switch to make it use 2-phase lookup (/Za for MSVC 7.1's cl for example).
Don't believe me? Try it out yourself ;-)


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

"Generic Usenet Account" <us****@sta.samsung.com> wrote in message
news:90*************************@posting.google.co m...
For a template class, all the inline functions are not checked by the
compiler for syntax errors until and unless those inline functions are
explicitly called by the user in the code. Don't believe me? Try it
out yourself ;-)


Of course, how else could it possibly work? How is the compiler supposed
to be able to recognize where the definitions end if it doesn't read them
syntactically.
Templates are not a macro processor, they are a deeply connected part of the
language syntax.
Jul 22 '05 #5
al***@start.no (Alf P. Steinbach) wrote in message news:<3f****************@News.CIS.DFN.DE>...
On 20 Dec 2003 16:56:14 -0800, us****@sta.samsung.com (Generic Usenet Account) wrote:
For a template class, all the inline functions are not checked by the
compiler for syntax errors
That is incorrect. The syntax is checked. Possibly you're confusing
"syntax" with higher level meaning.


At instantiation the compiler must check whether the template function
is meaningful, e.g., if it uses foo.bar, then foo.bar must exist, have
a type compatible with the usage, and so forth.

For the benefit of Alf and other skeptics, I am attaching some source
code below. There is a deliberate reference to the undefined variable
"skepticsdontbelieveit" in the overloaded "=" method definition.

As long as the following statement remains commented, I am not getting
any compiler error:
x = "Disprove the skeptics";
Here's the information on the compilers that I am using:
gcc version 2.95.3 20010315 (release)
CC: WorkShop Compilers 5.0 02/05/22 C++ 5.0 Patch 107311-18 [native
Sun compiler]
Actually the native Sun compiler is an even bigger culprit. As long
as the
x = "Disprove the skeptics";
statement remains commented, it does not even complain if the
statement terminator semicolon in
skepticsdontbelieveit += 2;
is omitted.
Regards,
KP Bhat
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FSString.h ~~~~~~~~~~~~~~~~~~~~~~~~
#ifndef _FSSTRING_H_
#define _FSSTRING_H_
// Fixed sized string
// Adapted from the class string (as defined in "The C++ Programming
Language"
// by Bjarne Stroustrup)
//
template<size_t size> class FSString
{
public:
FSString(const char* s)
{
strcpy(_data, s);
if(strlen(s) > size -1)
#ifdef DEBUG
cerr << "Truncating " << s << " to " << size << " characters."
<< end]l;
#endif
_data[size-1] = '\0'; // Just to be safe
}
FSString()
{
memset(_data, '\0', size);
}
FSString(const FSString& x)
{
strcpy(_data, x._data);
_data[size-1] = '\0'; // Just to be safe
}
~FSString()
{
}
FSString& operator=(const char* s)
{
skepticsdontbelieveit += 2; // Do you still have doubts?
strcpy(_data, s);
if(strlen(s) > size -1)
#ifdef DEBUG
cerr << "Truncating " << s << " to " << size << " characters."
<< endl;
#endif
_data[size-1] = '\0'; // Just to be safe
return *this;
}

//
// Lots of methods excluded
//

protected:
char _data[size];
};

#endif // _FSSTRING_H_
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FSStringTester.cpp ~~~~~~~~~~~~~~~~
#include <string.h>
#include "FSString.h"

main()
{

FSString<20> x("Show your magic");

//x = "Disprove the skeptics";
}
Jul 22 '05 #6
"Generic Usenet Account" <us****@sta.samsung.com> wrote in message
news:90*************************@posting.google.co m...
al***@start.no (Alf P. Steinbach) wrote in message news:<3f****************@News.CIS.DFN.DE>...
On 20 Dec 2003 16:56:14 -0800, us****@sta.samsung.com (Generic Usenet Account) wrote:
For a template class, all the inline functions are not checked by the
compiler for syntax errors


That is incorrect. The syntax is checked. Possibly you're confusing
"syntax" with higher level meaning.

At instantiation the compiler must check whether the template function
is meaningful, e.g., if it uses foo.bar, then foo.bar must exist, have
a type compatible with the usage, and so forth.

For the benefit of Alf and other skeptics, I am attaching some source
code below. There is a deliberate reference to the undefined variable
"skepticsdontbelieveit" in the overloaded "=" method definition.

As long as the following statement remains commented, I am not getting
any compiler error:
x = "Disprove the skeptics";

Here's the information on the compilers that I am using:
gcc version 2.95.3 20010315 (release)
CC: WorkShop Compilers 5.0 02/05/22 C++ 5.0 Patch 107311-18 [native
Sun compiler]


And here's what I got with the EDG front end at our web site
(adding a missing include):

"sourceFile.cpp", line 47: error:
identifier "skepticsdontbelieveit" is undefined
skepticsdontbelieveit += 2; // Do you still have doubts?

^

Do you still have doubts?
Actually the native Sun compiler is an even bigger culprit. As long
as the
x = "Disprove the skeptics";
statement remains commented, it does not even complain if the
statement terminator semicolon in
skepticsdontbelieveit += 2;
is omitted.


You're mining existing compilers for their current behavior, and
thinking you're unearthing golden nuggets of truth about how
Standard C++ is supposed to behave. If you want to do experiments
of that sort that are meaningful, stick with the EDG front end.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #7

"P.J. Plauger" <pj*@dinkumware.com> wrote in message news:L3************@nwrddc03.gnilink.net...
You're mining existing compilers for their current behavior, and
thinking you're unearthing golden nuggets of truth about how
Standard C++ is supposed to behave. If you want to do experiments
of that sort that are meaningful, stick with the EDG front end.


He's also confusing syntax with diagnosable semantic errors. The syntax has to
be checked at least to find where the end of the template definition is.

Jul 22 '05 #8

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

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
12
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
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 ]
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.