By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

A not very well publicized fact about C++ templates

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.