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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:01:15+00:00 2026-06-15T17:01:15+00:00

When I compile this program with either gcc-4.6.3 or gcc-4.7.2 the compiler gives me

  • 0

When I compile this program with either gcc-4.6.3 or gcc-4.7.2 the compiler gives me an error about the overloaded call being ambiguous:

#include <iostream>
#include <functional>

class Scott
{
    public:
        void func(const bool b = true)
        {
            std::cout << "Called func() with a boolean arg" << std::endl;
        }

        void func(std::function<void(void)> f)
#ifdef WITH_CONST
            const
#endif
        {
            std::cout << "Called func() with a std::function arg" << std::endl;
        }
};


int main (int argc, char *argv[])
{
    Scott s;
    s.func([] (void) { });
}

However, if I make the overloaded function const, it compiles fine & calls the method I did not expect!

devaus120>> g++ -Wall -std=c++11 -DWITH_CONST wtf.cxx
devaus120>> ./a.out
Called func() with a boolean arg

So, I have 2 questions:

  1. Is it a compiler bug that this compiles when the overloaded method is made const?
  2. How can I ensure the correct overloaded function gets invoked? (Need to cast the argument somehow?)

TIA.

Scott. 🙂

  • 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-06-15T17:01:16+00:00Added an answer on June 15, 2026 at 5:01 pm

    Actually gcc is correct! Because lambda is not a function but a closure object of class type! Really! You can even inherit from it 🙂 … even multiple times from different lambdas…

    So, according 8.5/16:


    […]

    — If the destination type is a (possibly cv-qualified) class type:

    […]

    — Otherwise, if the source type is a (possibly cv-qualified) class type, conversion functions are considered. The applicable conversion functions are enumerated (13.3.1.5), and the best one is chosen through overload resolution (13.3). The user-defined conversion so selected is called to convert the initializer expression into the object being initialized. If the conversion cannot be done or is ambiguous, the initialization is ill-formed.


    and 13.3.1.5:


    Under the conditions specified in 8.5, as part of an initialization of an object of nonclass type, a conversion function can be invoked to convert an initializer expression of class type to the type of the object being initialized. Overload resolution is used to select the conversion function to be invoked. Assuming that “cv1 T” is the type of the object being initialized, and “cv S” is the type of the initializer expression, with S a class type, the candidate functions are selected as follows:

    — The conversion functions of S and its base classes are considered. Those non-explicit conversion functions that are not hidden within S and yield type T or a type that can be converted to type T via a standard conversion sequence (13.3.3.1.1) are candidate functions. For direct-initialization, those explicit conversion functions that are not hidden within S and yield type T or a type that can be converted to type T with a qualification conversion (4.4) are also candidate functions. Conversion functions that return a cv-qualified type are considered to yield the cv-unqualified version of that type for this process of selecting candidate functions. Conversion functions that return “reference to cv2 X” return lvalues or xvalues, depending on the type of reference, of type “cv2 X” and are therefore considered to yield X for this process of selecting candidate functions.


    so finally, result of conversion function is a function pointer which would implicitly converted to bool…

    you may check this series of conversions with the following simple code:

    #include <iostream>
    #include <iomanip>
    
    int main()
    {
        std::cout << std::boolalpha << []{ return 0; } << '\n';
    }
    

    the output will be true…

    Here is few ways to workaround… you definitely need something because both functions are suitable after overload resolution. Btw, adding const, to signature of the second one, just exclude it because you’ve got a mutable instance of Scott and again you’ll get compile error if declare it w/ const modifier.

    So, you can do:

    • explicit cast (as mentioned in comments)… yeah, long to type…
    • declare the second foo w/ template parameter Func. Depending on what you are going to do then, here is few options: it can be converted to std::function on assign (if you want to store it to some member), or in case of immediate call you’ll even get some optimization (by eliminate conversion to std::function)
    • more complex way is to declare both functions w/ template parameter and use std::enable_if to turn one of them OFF depending on std::is_same<bool, T> for example (or check for callable/function type)
    • use type dispatching (yeah, again w/ templated functions)

    … I guess it’s enough 🙂

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

Sidebar

Related Questions

when I'm trying to compile my c program it gives me this error warning:
I was a little confused by this expression: gcc -c -g program.c >& compiler.txt
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
Have tried to compile and run this program, but have received this error message
I'm trying to compile this simple program to start learning how to use timers:
I am trying to compile a one-off script, an autogenerated C# program. This program
When I attempt to compile the output of this trivial lex program: # lex.l
This program in C runs and compiles well : #ifdef HAVE_CONFIG_H #include <config.h> #endif
I tried to compile this example: #include <stdio.h> #include <stdlib.h> #include <stddef.h> main(){ size_t
int main() { int i=3; (++i)++; printf(%d,i); } This programs works with g++ compiler

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.