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 6826007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:05:50+00:00 2026-05-26T22:05:50+00:00

Why might my compiler see the following GetLength function pointer as ambiguous pseudo-code: size_t

  • 0

Why might my compiler see the following GetLength function pointer as ambiguous
pseudo-code:

size_t GetLength(char*);
size_t GetLength(wchar_t*);
struct ITEM { };
double GetLength(ITEM*);

CString GetInfo(ITEM * item, std::function<double (ITEM*)> fn)
{
  ... omitted for clarity
}

ITEM * item = new ITEM;
cout << GetInfo(item, GetLength);  // <- ambiguous error

GetInfo only allows something of the double return + ITEM* argument pattern. So why is it considering (and not discarding) the two string based variations of GetLength?

  • 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-26T22:05:50+00:00Added an answer on May 26, 2026 at 10:05 pm

    The constructor for std::function<...> is templated because it has to be able to support any function-like input type. There’s no single type to try to deduce to, so your overloads are all possible to construct with; it wouldn’t be until later into compilation later that an error arose for a type mismatch.

    You could do this:

    GetInfo(item, static_cast<double(*)(ITEM*)>(GetLength));
    

    To explicitly discard the other overloads.


    In other words, it’s the same reason this won’t work:

    void foo(int);
    void foo(void*);
    
    struct bar
    {
        template <typename T>
        bar(T f)
        {
            f(5);
        }
    };
    
    bar b(foo);
    

    Even though the constructor body for bar will only work with void foo(int),
    it wants to support any function where f(5) will work so the argument type is templated. This allows any function to work in that place, which means the compiler cannot deduce a single best overload to use.


    I think that one language-level solution is for an overload set to actually be a functor itself. That is, given:

    void foo(int);
    void foo(void*);
    
    template <typename T>
    double foo(int, T);
    

    Naming foo (as in bar(foo) or even just foo(5)) results in an instance of this type:

    struct __foo_overload_set // internal
    {
        // forwarders
        void operator()(int __arg0) const
        {
            // where __foo0 is the zeroth overload, etc...
            return __foo0(__arg0);
        }
    
        void operator()(void* __arg0) const
        {
            return __foo1(__arg0);
        }
    
        template <typename __Arg1>
        double operator()(int __arg0, __Arg1&& __arg1) const
        {
            return __foo2(__arg0, std::forward<__Arg1>(__arg1));
        }
    
        // converters
        typedef void(*__foo0_type)(int);
    
        operator __foo0_type() const
        {
            return __foo0;
        }
    
        typedef void(*__foo1_type)(void*);
    
        operator __foo1_type() const
        {
            return __foo1;
        }
    
        template <typename T>
        struct __foo2_type
        {
            typedef void(*type)(int, T);
        };
    
        template <typename T>
        operator typename __foo2_type<T>::type() const
        {
            return __foo2;
        }
    };
    

    Which, being callable itself, will compile in the context we want. (AFAIK, it does not introduce any ambiguities that don’t already exist, though it’s completely untested.)

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

Sidebar

Related Questions

The following code prints out 3, not 4 as you might expect. public class
This might be iPhone specific, I'm not sure. The compiler doesn't complain when building
I have the following function (which worked in Visual Studio): bool Plane::contains(Vector& point){ return
I have the following code which compiles and runs fine under Visual Studio 2008
In C++ I can do the following: template<typename T, typename V> struct{ void operator()(T
Greetings, I'm trying to download a web page with the following code: public partial
Possibly related to my last question (note: different error code): Why might the fatal
I have code of the following form: class Test { private final A t;
Possible Duplicate: When do you use code blocks? Ok, this might be a stupid
Might be subjective and/or discussion.. but here goes. I've been asked to estimate 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.