Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4233906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:16:50+00:00 2026-05-21T02:16:50+00:00

I have a very similar problem to that presented by Morpheus, in the following

  • 0

I have a very similar problem to that presented by Morpheus, in the following question:

Overloaded member function pointer to template

The solution proposed by Richard Corden requires the user to explicitly specify the function parameter type, to distinguish it among the overloads. However, this solution does not seem to work with overloads with a varying number of arguments of the same type.

Consider this example (derived from the original question):

template <typename T>
class A
{
public:
  template<class Arg1>
  void connect(void (T::*f)(Arg1)) 
  {
    //Do some stuff
  }

  template<class Arg1, class Arg2>
  void connect(void (T::*f)(Arg1,Arg2)) 
  {
    //Do some stuff
  }

  void connect(void (T::*f)()) 
  {
    //Do some stuff
  }
};

class GApp
{
public:
    void foo() {}
    void foo(double d) {}
    void foo(double d1, double d2) {}
};


int main ()
{
  A<GApp> a;
  a.connect (&GApp::foo);                // foo () - OK
  a.connect<double> (&GApp::foo);        // foo (double) - FAIL
  a.connect<double,double> (&GApp::foo); // foo (double,double) - OK
}

GNU G++ 3.4.5 and MSVC 2008 do not compile the code above, both presenting similar error messages:

test.cpp: In function `int main()':
test.cpp:36: error: call of overloaded `connect(<unknown type>)' is ambiguous
test.cpp:7: note: candidates are: void A<T>::connect(void (T::*)(Arg1)) [with Arg1 = double, T = GApp]
test3.cpp:13: note:               void A<T>::connect(void (T::*)(Arg1, Arg2)) [with Arg1 = double, Arg2 = double, T = GApp]

I’m aware of some workarounds that can make it compile, like assigning the pointer to a variable of the exact same type (e.g. void (GApp::*tmp)(double) = &GApp::foo;) or, using a more explicity form when calling the connect function (e.g. connect((void (GApp::*)(double))(&GApp::foo));).

However, I prefer the first solution and would like to know why it is not working.

Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:16:51+00:00Added an answer on May 21, 2026 at 2:16 am

    For a.connect<double> (&GApp::foo), both the foo(double) and foo(double, double) will match the overload of connect with one, and with two template parameters respectively (in the case of the two parameter version, the second template argument will be deduced, the first argument was provided by you explicitly).

    If you want to disambiguate ambiguous cases, I recommend that you pass the exact type, so no surprises can happen. Instead of those overloads, why not have a single overload

    template<typename MType, typename T>
    void connect(MType T::*f) 
    {
      //Do some stuff
    }
    
    a.connect<void()> (&GApp::foo);
    a.connect<void(double)> (&GApp::foo);
    a.connect<void(double, double)> (&GApp::foo);
    

    The last call to connect should work fine in your code too. You then can dissect the type MType in connect with separate templates, to get the parameter and return types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this is a repeated question. I have found very similar questions
I am working with data.frames that have very similar names (df1, df2,.. df7). Because
I have a couple very similar looking folders on my machine that correspond to
I have tried my best to understand a very similar StackOverflow question , but
I have 2 tables in a DB with very similar schemas that need to
I have a test setup where I have many very similar unit tests that
I have a problem very similar to this one , but slightly more involved.
I've worked on projects that have very complex XML configuration, and one problem that's
I have a very similar problem to the post located here: Telerik grid with
I have a very similar problem as described here . I also upgraded a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.