A not very well publicized fact about C++ templates

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

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
 
A

Alf P. Steinbach

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

John Carson

Generic Usenet Account said:
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.
 
R

Rob Williscroft

Generic Usenet Account wrote in
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.
 
R

Ron Natalie

Generic Usenet Account said:
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.
 
G

Generic Usenet Account

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";
}
 
P

P.J. Plauger

(e-mail address removed) (Alf P. Steinbach) wrote in message
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
 
R

Ron Natalie

P.J. Plauger said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top