Topic: Usage of Template Based functions as friend


Author: girish.shetty@nokia.com ("Girish Shetty")
Date: Fri, 15 Jul 2005 05:38:28 GMT
Raw View
I have met with some C++ Standard Problem among different Compilers

The problem is with having template based functions/class as friend to a
class.

Suppose that I have some code like this:

Windows/Linux Version of the Code:

class Sample {

public:

template<typename Type> friend void WorkOnThis(Type& aType);

/* But below statement wont work with Microsoft C++ Compiler !!

template<typename Type> friend class ChangeThis; */

Sample(int aVal=0) : iVal(aVal) { }

~Sample() { }

void Display () const { cout<<"Value is : "<<iVal<<endl; }

private:

int iVal;

};

template<typename Type> void WorkOnThis(Type& aType) {

aType.iVal = 200;

}

This gets complied cleanly with Microsoft C++ Complier as well as Unix (CC
or GCC)

But,

Symbian Version of the Code:

#include <e32def.h>

class TSample

{

public:

TSample(TInt aVal=0) : iVal(aVal) { }

~TSample() { }

template<typename Type> friend void WorkOnThis(Type& aType);

template<typename Type> friend class TChangeThis;

/* But if you replace above 2 statements with below 2, It Works

friend void WorkOnSample(TSample& aSam);

friend class TChangeSample; */

private:

TInt iVal;

};

template<typename Type> void WorkOnThis(Type& aType)

{

aType.iVal = 200;

}

Here, when I build it for winscw(which maked use of RVCT Compiler), I will
end up with these errors !!! :

Sample.cpp

Sample.cpp:8: declaration syntax error

Sample.cpp:10: declaration syntax error

Sample.cpp:13: declaration syntax error

Sample.cpp:15: declaration syntax error



Similarly, when I use Template based class as friend in MSC++ environment,
its a error there ( See above commented codse for MSC++)



Regards

Girish



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]