BUG: instantiating templates with typedef ('operator...' functions)
Nick
m@loa.com
Sat Jul 15 21:59:00 GMT 2000
//
// This example shows the problem of instantiating the 'operator=='
function
// using a typedef. Any other function beside that operator instantiates
// correctly.
//
//
// To see the problem, compile as
//
// g++ -c test.C -DSHOW
//
// To see how other functions are doing fine, compile as
//
// g++ -c test.C
//___________________________________________________________________________
//
// WORKAROUND:
//
// The solution for this is not to use the typedef, but the original
class.
// In this particular example, omu_PrimIntDictionary will not work, but
// the omu_PrimDictionary<int> will. Note that omu_PrimIntDictionary is
// just a typedef of omu_PrimDictionary<int>.
//___________________________________________________________________________
//
// Submitter: Nick Monyatovsky <mon@hks.com> or <m@loa.com>
// Date: 2000/07/15
// Version: gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2
release)
// System: Linux 2.2.14-5.0 (RedHat 6.1 on Intel Pentium III)
//___________________________________________________________________________
#if defined(SHOW)
# define PROBLEM_FUNCTION operator==
#else
# define PROBLEM_FUNCTION prettyPrint
#endif
#include <iostream.h>
class omu_Primitive {
public:
virtual bool PROBLEM_FUNCTION(const omu_Primitive &) const;
protected:
int type;
};
template <class T>
class omu_PrimDictionary : public omu_Primitive
{
public:
virtual bool PROBLEM_FUNCTION(const omu_Primitive& arg) const;
};
template <class T>
bool omu_PrimDictionary<T>::PROBLEM_FUNCTION(const omu_Primitive& arg)
const
{
if (type > 0)
return true;
else
return false;
}
//////////////////////////// PROBLEM HERE /////////////////////////////
//
// operator== function does not want to instantiate itself correctly.
// Any other function in its place instantiates itself
// as omu_PrimDictionary<int>..., but not the operator==.
//
// SOLUTION: Do not use the typedef. Use the underlying type directly.
//
///////////////////////////////////////////////////////////////////////
typedef omu_PrimDictionary<int> omu_PrimIntDictionary;
template <>
//bool omu_PrimDictionary<int>::PROBLEM_FUNCTION(const omu_Primitive&
arg) const
bool omu_PrimIntDictionary::PROBLEM_FUNCTION(const omu_Primitive& arg)
const
{
if (type > 0)
return true;
else
return false;
}
More information about the Gcc-bugs
mailing list