I tried to remake the program and provide examples in an attempt to reproduce the problem on Microsoft Visual C++ 2005/2008 compiler, but I different errors than the one I expected despite the tests--
/**
* Assumed header file for LogBuffer
*/
#ifndef LOGBUFFER_H
#define LOGBUFFER_H
#include <iostream>
/* Dummy values */
typedef char LogFactor;
typedef int uint8_t;
template<class T>class MsgBufferArray;
typedef char LoggerFunctor;
typedef char ostringstream;
using std::ios;
namespace Log{
class LogBuffer;
template <class T>
void trace(LogBuffer& msgBuffer, T& data);
class LogBuffer{
public:
LogBuffer(LogFactor& logFactor);
~LogBuffer();
uint8_t* GetStart();
size_t GetProcessedSize();
void Flush();
void SetFormat(ios::fmtflags flags, ios::fmtflags mask);
uint8_t GetIndent();
void SetIndent(uint8_t indent);
// Generic flat type
template <class T>
LogBuffer& operator<<(T& data);
// Single element pointer
template <class T>
LogBuffer& operator<<(T* & data);
// Array
template <class T>
LogBuffer& operator<<(const MsgBufferArray<T>& data);
protected:
void SetDefaultFormat();
//LoggerFunctor& _functor;
ostringstream _buffer;
uint8_t _indent;
};
}
#include "LogBuffer.cpp"
#endif /**
* Assumed implementation file of LogBuffer
*/
#ifdef LOGBUFFER_H
using namespace Log;
LogBuffer::LogBuffer(LogFactor& logFactor){}
LogBuffer::~LogBuffer(){}
template <class T>
LogBuffer& LogBuffer::operator<<(T & data){
::trace(*this, data); // trace fuction called here
return *this;
}
template <class T>
void trace(LogBuffer& msgBuffer, T& data){
data.trace(msgBuffer);
}
#endif /**
* Driver program for testing--
*/
#include <iostream>
#include "LogBuffer.h"
using std::cout;
using std::cin;
using std::endl;
class Tracer{
public:
Tracer(){}
void trace(LogBuffer& msgBuffer){
cout << "Trace successful" << endl;
}
};
class NonTracer{
public:
NonTracer(){}
char trace;
};
int main(){
char refChar = '\0'; // dummy variable
Tracer myTracer; // object of type Tracer, has the method trace
NonTracer myNonTracer; // object of type NonTracer, doesn't have trace function but a trace mbmer
Tracer myOtherTracer(); // pointer to a function that accepts no arguments and returns a Tracer
LogBuffer lb (refChar); // making a LogBuffer object and passing it the dummy variable
lb << myTracer; // works
// lb << myNonTracer; // obviously doesn't work, but doesn't reproduce desired error
// lb << myOtherTracer; Error 1 error C2228: left of '.trace' must have class/struct/union h:\visual studio 2008\projects\testingtemplates__\testingtemplates__\tesitngtemplate__t.cpp 35 TestingTemplates__
// myOtherTracer.trace(LogBuffer(refChar)); Error 1 error C2228: left of '.trace' must have class/struct/union h:\visual studio 2008\projects\testingtemplates__\testingtemplates__\tesitngtemplate__t.cpp 35 TestingTemplates__
cin.ignore();
cin.get();
return 0;
}
Perhaps the errors differ from compiler to compiler, or it may be possible that the examples provided above were not adjacent to the example in the link?
The error seems to occur in gcc (and/or g++?) type compilers so I think the error stated above may be the same thing though I can't confirm it without experience with those compilers.