Specifying address of member function template problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Specifying address of member function template problem

  1. #1
    Join Date
    Dec 2003
    Location
    Atlanta, Georgia
    Posts
    11

    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 ?
    Edward Diener

  2. #2
    Join Date
    Oct 2008
    Posts
    1,090

    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 )

    "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 ...
    Last edited by superbonzo; March 15th, 2011 at 04:20 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center