Function Template help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Rubinsteyn

    #1

    Function Template help

    Hi,
    I can't figure out why my function templates don't resolve properly.
    Anyone here want to help me out?

    [code]
    ---
    template <typename T>
    Obj* call_method (T* t, void (T::*f)())
    {
    (t->*f)();
    return Void;
    }

    template <typename T, typename Ret>
    Obj* call_method (T* t, Ret (T::*f)())
    {
    return cpp_to_hyrax((t->*f)());
    }
    ....
    I instantiate the template with:
    ---
    T* t = new T;
    Obj* o = call_method<T, void(t, &T::func);
    ---
    T::func's signature is void().
    It seems to me that the first template ought to be instantiated, but
    it's the second one that wins. Any clues why?

    Thanks for any help,
    -Alex

  • Alex Rubinsteyn

    #2
    Re: Function Template help

    Also, I tried:
    call_method (t, &T::func);
    But the compiler tells me that call_method is ambiguous.

    Comment

    • Ian Collins

      #3
      Re: Function Template help

      Alex Rubinsteyn wrote:
      Hi,
      I can't figure out why my function templates don't resolve properly.
      Anyone here want to help me out?
      >
      [code]
      Please post enough to compile.
      ---
      template <typename T>
      Obj* call_method (T* t, void (T::*f)())
      {
      (t->*f)();
      return Void;
      }
      >
      template <typename T, typename Ret>
      Obj* call_method (T* t, Ret (T::*f)())
      {
      return cpp_to_hyrax((t->*f)());
      }
      ....
      I instantiate the template with:
      ---
      T* t = new T;
      Obj* o = call_method<T, void(t, &T::func);
      Why not

      Obj* o = call_method(t, &T::func);

      Which will give you the expected result.
      ---
      T::func's signature is void().
      It seems to me that the first template ought to be instantiated, but
      it's the second one that wins. Any clues why?
      >
      You are explicitly specifying two template types.

      --
      Ian Collins.

      Comment

      • Alex Rubinsteyn

        #4
        Re: Function Template help

        Hi Ian,
        Please post enough to compile.
        Enough to compile...is a rather lot. I can send you the Visual Studio
        project if you're interested.
        Why not
        >
        Obj* o = call_method(t, &T::func);
        >
        Which will give you the expected result.
        You are explicitly specifying two template types.
        Sorry for the omission, but see my second post. I ended up trying to
        specify the template types because call_method(t, &T::func) is (for
        some reason) ambiguous. So I guess my question is, how I get the the
        first call_method to be instantiated?

        Thanks,
        Alex

        Comment

        • Alex Rubinsteyn

          #5
          Re: Function Template help

          Whatever the problem was, I've found a way around it by using partially
          specialized functor classes instead of a function. Thanks anyways for
          the help.

          -Alex

          Comment

          Working...