Problem with function pointer template args.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

What?s wrong with the following program (ignoring the stdio.h include!).
When I compile this program with gcc (4.1.2) I get the following error
message.

gccTest.cpp: In member function 'int CMyTemplate<TYPE>::aMethod(const TYPE&)
[with TYPE = MyNamespace::CMyClass1 (*)()]':
gccTest.cpp:45:   instantiated from here
gccTest.cpp:14: error: call of overloaded 'myHash(MyNamespace::CMyClass1 (*
const&)())' is ambiguous
gccTest.cpp:4: note: candidates are: int myHash(int) <near match>
gccTest.cpp:5: note:                 int myHash(char) <near match>

For some reason the compiler does not like my declaration of myHash with
argument pMyFunc. However if the return type from pMyFunc is changed to a
class in the global namespace then it works ok. I think that there is a bug
with the compiler when handling function pointer template arguments. It
looks like the compiler cannot cope with having a function pointer return
type which is not in the global namespace. Either that or there is something
about types in C++ that I don?t understand.

This was discovered in a match larger piece of code but I?ve constructed
this simple example to highlight the problem. Just try to compile with gcc
gccTest.cpp. I?m using gcc under Slackware 12 BTW if that makes any
difference.

Regards,

Paul H

Here?s the program that fails??

#include <stdio.h>

// Some inline functions for the hash function.
inline int myHash(int a) {return a;}
inline int myHash(char a) {return a;}

// My simple template class.
template <class TYPE>
class CMyTemplate
{
public:
      int aMethod(const TYPE& a)
      {
      return myHash(a);
      }
};

// Here is a class in a namespace.
namespace MyNamespace
{
class CMyClass1 {};
}

// Here is a class in the global namespace.
class CMyClass2 {};

// Typedef a function pointer that points to a function with no args but
returns a CMyClass1.
// If you comment CMyClass1 and put in CMyClass2 as the return type then it
works.
typedef MyNamespace::CMyClass1 (*pMyFunc)();
// typedef CMyClass2 (*pMyFunc)();

// Here is my user provided hash function for a pMyFunc pointer. Just return
the pointer.
inline int myHash(pMyFunc a)
{
      return reinterpret_cast<int>(a);
}

int main(int argc, char*argv[])
{
      // Create an instance of CMyTemplate with TYPE=pMyFunc.
      typedef CMyTemplate<pMyFunc> CMyTemplateInst;
      CMyTemplateInst x;

      // Instantiate some code by calling aMethod. This will not compile if
we use CMyClass1, but CMyClass2 is ok!
      pMyFunc a=0;
      x.aMethod(a);

      return 0;
}




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux