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