473,794 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1563
On 20 Dec 2003 16:56:14 -0800, us****@sta.sams ung.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.sam sung.com> wrote in message
news:90******** *************** **@posting.goog le.com
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.goog le.com:
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.sam sung.com> wrote in message
news:90******** *************** **@posting.goog le.com...
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.sams ung.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
"skepticsdontbe lieveit" 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
skepticsdontbel ieveit += 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)
{
skepticsdontbel ieveit += 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.sam sung.com> wrote in message
news:90******** *************** **@posting.goog le.com...
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.sams ung.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
"skepticsdontbe lieveit" 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 "skepticsdontbe lieveit" is undefined
skepticsdontbel ieveit += 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
skepticsdontbel ieveit += 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.g nilink.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
2570
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 header is the same in each page and is described in an XML document, "Head.xml". The document part, which is variable in content, is described in other XML files (e.g. "Document.xml", "Product.xml", "Register.xml").
5
2536
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 of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
22
2197
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 solutions which are overly specific),
12
1970
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 *.cpp). What amazes me is that I have already some code like this in another project where I don't get errors, so I am pretty sure I am missing some stupid thing.
16
16292
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
1659
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 get! In the following i've copy pasted the example from <http://www.w3schools.com/xsl/xsl_apply_templates.asp> into this post. I divide into sections and my real question will come after the code snippet:
28
2642
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 executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
104
4624
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 feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
7
1712
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 remove all defined templates. I am not really a c++ guy so I have not worked with templates all that much (not really at all) and as such I don't have a good idea where to start.
0
9672
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
9519
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
10213
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
10163
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
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.