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

  • Home
  • SEARCH
  • 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 6159495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:08:34+00:00 2026-05-23T21:08:34+00:00

So, here is some basic code which illustrates my question: #include <functional> int func(int

  • 0

So, here is some basic code which illustrates my question:

#include <functional>

int func(int x) {
    return x;
}

int func(int x, int y) {
    return x + y;
}

int main() {
    std::ptr_fun<int, int>(func);
}

We have 2 overloads for a function with a different number of parameters. Then I attempt to turn the single parameter version in a functor. Of course, I am met with the following error:

test.cc: In function 'int main()':
test.cc:13:29: error: call of overloaded 'ptr_fun()' is ambiguous
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4/bits/stl_function.h:437:5: note: candidates are: std::pointer_to_unary_function std::ptr_fun(_Result (*)(_Arg)) [with _Arg = int, _Result = int]
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4/bits/stl_function.h:463:5: note:                 std::pointer_to_binary_function std::ptr_fun(_Result (*)(_Arg1, _Arg2)) [with _Arg1 = int, _Arg2 = int, _Result = int]

I know I could just cast func and be done with it, but it got me thinking why is this ambiguous? Neither version of std::ptr_fun have default parameters in the template definitions, and I have explicitly said that the two template parameters are int.

In fact, if I just do what the compiler is essentially doing during template instantiation time like this:

#include <functional>

int func(int x) {
    return x;
}

int func(int x, int y) {
    return x + y;
}

std::pointer_to_unary_function<int,int> my_ptr_fun (int (*f)(int)) {
  return std::pointer_to_unary_function<int,int>(f);
}

int main() {
    my_ptr_fun(func);
}    

It compiles just fine, somehow the ambiguity is gone! Anyone have any insite into why this is the case?

  • 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-23T21:08:35+00:00Added an answer on May 23, 2026 at 9:08 pm

    Its because when you call a templated function, you don’t have to specify any template parameters that can be inferred by the type of the function arguments. As a result, calling std::ptr_fun<int, int> doesn’t actually specify which of the std::ptr_fun overloads you calling, and it relies on the function you pass as an argument for resolution. As your func has overloads that fit for both std::ptr_fun overloads, there is ambiguity.

    Edit: Here is an example to demonstrate my point – ran on Ideone, it shows both function calls return the same type.

    #include <functional>
    #include <iostream>
    #include <typeinfo>
    
    double func(int x) 
    {
        return x;
    }
    
    int main() 
    {
        std::cout << typeid(std::ptr_fun<int>(func)).name() << std::endl;
        std::cout << typeid(std::ptr_fun<int, double>(func)).name() << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some basic questions about C++. Consider the following code in which I
Here is some basic code and its output. I really can't say anything more
Here is some code I could not get to format properly in markdown, this
Here's some Ruby code: puts %x{ pstree #{$$} } # never forks puts %x{
Here's some code I saw once. Can you see what's wrong with it? [updated]
I'm looking for something like break for loops. Here's some example code (using Symfony's
I currently have a program which has the following basic structure main function --
The following is some old ASMX code which I need to convert to WCF
Really stupid C question. I'm trying to build the source code here so I
I am looking for a basic pattern where I can move some shared code.

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.