# Specifying address of member function template problem

• March 14th, 2011, 04:32 PM
eldiener
Specifying address of member function template problem
Attempting to take the address of a member function template succeeds when the enclosing class is hardcoded but fails when it is passed to a template. The code:

Code:

```struct AType   {   double SomeFunc(int,long *,double &) { double ret(0); return ret; }   template<class X,class Y,class Z,short AA> double SomeFuncTemplate(X,Y *,Z &) { double ret(AA); return ret; }   }; template   <   class T   > struct TTest   {   typedef char Bad;   struct Good { char x[2]; };   template<T> struct helper;   static Good check(helper<&AType::template SomeFuncTemplate<int,long,double,50> > *);   static Bad check(...);   static const bool value=sizeof(check(0))==sizeof(Good);   };   template   <   class T,   class C   > struct TTest1   {   typedef char Bad;   struct Good { char x[2]; };   template<T> struct helper;   template<class U> static Good check(helper<&U::SomeFunc> *);   template<class U> static Bad check(...);   static const bool value=sizeof(check<C>(0))==sizeof(Good);   };   template   <   class T,   class C   > struct TTest2   {   typedef char Bad;   struct Good { char x[2]; };   template<T> struct helper;   template<class U> static Good check(helper<&U::template SomeFuncTemplate<int,long,double,50> > *);   template<class U> static Bad check(...);   static const bool value=sizeof(check<C>(0))==sizeof(Good);   };   int main()   {     static_assert(TTest                   <                   double (AType::*)(int,long *,double &)                   >::value,                 "failure in TTest"               );               static_assert(TTest1                   <                   double (AType::*)(int,long *,double &),                   AType                   >::value,                 "failure in TTest1"               );               static_assert(TTest2                   <                   double (AType::*)(int,long *,double &),                   AType                   >::value,                 "failure in TTest2"               );               return 0;     }```
A static assert occurs for TTest2 of "failure in TTest2" but there is no static assert for TTest0 or TTest1. Tried wth gcc 4.5.2 and msvc 10.0.

The TTest1 shows that the same technique used in the failing TTest2 is used with a regular member function, and succeeds.

Why is this failure occuring ? Is it a bug in both compilers or is my code for TTest2 incorrect somehow ?
• March 15th, 2011, 03:45 AM
superbonzo
Re: Specifying address of member function template problem
... uhm, your test code is not quite right ... anyway, the following code

Code:

```struct A {   template<class X,class Y,class Z,short W> double f(X,Y *,Z &); }; template <class T> struct B {         typedef double (T::*FunPtr)(int,long*,double&);         static FunPtr Get() { return &T::template f<int,long,double,50>; } }; int main() {   B<A>::Get();   return 0; }```
compiles ok on the Comeau compiler. Is this what you are looking for ?

EDIT: actually, I reread your test code and it seems ok ... indeed, the code

Code:

```struct A {   template<class X,class Y,class Z,short W> double f(X,Y *,Z &); }; template   <   class T,   class C   > struct TTest2   {   typedef char Bad;   struct Good { char x[2]; };   template<T> struct helper;   template<class U> static Good check(helper<&U::template f<int,long,double,50> > *);   template<class U> static Bad check(...);   static const bool value=sizeof(check<C>(0))==sizeof(Good);   }; static_assert( TTest2<double (A::*)(int,long *,double &),A>::value,  "failure in TTest2" );```
gives the following horrible error on Comeau ( which is one of the most standard compliant compiler )

Quote:

"ComeauTest.c", line 18: internal error: assertion failed at: "templates.c", line
5155

static const bool value=sizeof(check<C>(0))==sizeof(Good);
^

1 catastrophic error detected in the compilation of "ComeauTest.c".
Compilation aborted.
so, it seems that current compilers cannot handle such kind of type deduction correctly ...