Compiler only complains about the ambiguous overloaded functions when the parameter is 0Function overloading in Javascript - Best practicesCan we overload main() function in C++?How to resolve ambiguous overloaded function call?The Ambiguous Overloading in JavaAmbiguous overload for operator+ using conversionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsFunction overloading by function pointerC++ - How does the compiler decide between overloaded functions with reference types as parameter?Why does the compiler complain for ambiguity in overloaded function?Resolution of function overloading about initializer_list

How to interpret a promising preprint that was never published?

Why were these characters absent in Spider-Man: Far From Home?

Which modern firearm should a time traveler bring to be easily reproducible for a historic civilization?

Round command argument before using

How can I help our ranger feel special about her beast companion?

Which GPUs to get for Mathematical Optimization (if any)?

Why aren't there any women super GMs?

Term “console” in game consoles

"Je suis petite, moi?", purpose of the "moi"?

The most secure way to handle someone forgetting to verify their account?

Software need db owner permission to master database (sql2016)

How to draw a winding on a toroid of a circular cross section?

Difference between c++14 and c++17 using: `*p++ = *p`

Why does a tetrahedral molecule like methane have a dipole moment of zero?

What happens if a company buys back all of its shares?

Is it legal for a supermarket to refuse to sell an adult beer if an adult with them doesn’t have their ID?

What are the basics of commands in Minecraft Java Edition?

Why don't humans perceive waves as twice the frequency they are?

Operation Unzalgo

Word for something indicating the importance of guarding it properly

How did Jayne know when to shoot?

Arithmetics in LuaLaTeX

Applying for jobs with an obvious scar

Zhora asks Deckard: "Are you for real?" Was this meant to be significant?



Compiler only complains about the ambiguous overloaded functions when the parameter is 0


Function overloading in Javascript - Best practicesCan we overload main() function in C++?How to resolve ambiguous overloaded function call?The Ambiguous Overloading in JavaAmbiguous overload for operator+ using conversionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsFunction overloading by function pointerC++ - How does the compiler decide between overloaded functions with reference types as parameter?Why does the compiler complain for ambiguity in overloaded function?Resolution of function overloading about initializer_list






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








10















I fixed a bug recently.



In the following code, one of the overloaded function was const and the other one was not. The issue will be fixed by making both functions const.



My question is why compiler only complained about it when the parameter was 0.



#include <iostream>
#include <string>

class CppSyntaxA

public:
void f(int i = 0) const i++;
void f(const std::string&)
;

int main()

CppSyntaxA a;
a.f(1); // OK
//a.f(0); //error C2666: 'CppSyntaxA::f': 2 overloads have similar conversions
return 0;










share|improve this question



















  • 2





    @NeilButterworth GCC shows the same behavior.

    – Miles Budnek
    9 hours ago











  • @Miles No, it doesn't.

    – Neil Butterworth
    9 hours ago











  • @NeilButterworth sure does: wandbox.org/permlink/mEtMFPt5KMdSVGs3 and wandbox.org/permlink/8UiG8XD7d0ol8U8v

    – Parsa
    9 hours ago












  • clang seems happy with it for what that's worth.

    – tadman
    9 hours ago











  • Obviously, I can't illustrate the lack of an error message, but GCC 8.1.0 compiles it with both -std=c++11 and -std=c++17

    – Neil Butterworth
    8 hours ago

















10















I fixed a bug recently.



In the following code, one of the overloaded function was const and the other one was not. The issue will be fixed by making both functions const.



My question is why compiler only complained about it when the parameter was 0.



#include <iostream>
#include <string>

class CppSyntaxA

public:
void f(int i = 0) const i++;
void f(const std::string&)
;

int main()

CppSyntaxA a;
a.f(1); // OK
//a.f(0); //error C2666: 'CppSyntaxA::f': 2 overloads have similar conversions
return 0;










share|improve this question



















  • 2





    @NeilButterworth GCC shows the same behavior.

    – Miles Budnek
    9 hours ago











  • @Miles No, it doesn't.

    – Neil Butterworth
    9 hours ago











  • @NeilButterworth sure does: wandbox.org/permlink/mEtMFPt5KMdSVGs3 and wandbox.org/permlink/8UiG8XD7d0ol8U8v

    – Parsa
    9 hours ago












  • clang seems happy with it for what that's worth.

    – tadman
    9 hours ago











  • Obviously, I can't illustrate the lack of an error message, but GCC 8.1.0 compiles it with both -std=c++11 and -std=c++17

    – Neil Butterworth
    8 hours ago













10












10








10








I fixed a bug recently.



In the following code, one of the overloaded function was const and the other one was not. The issue will be fixed by making both functions const.



My question is why compiler only complained about it when the parameter was 0.



#include <iostream>
#include <string>

class CppSyntaxA

public:
void f(int i = 0) const i++;
void f(const std::string&)
;

int main()

CppSyntaxA a;
a.f(1); // OK
//a.f(0); //error C2666: 'CppSyntaxA::f': 2 overloads have similar conversions
return 0;










share|improve this question
















I fixed a bug recently.



In the following code, one of the overloaded function was const and the other one was not. The issue will be fixed by making both functions const.



My question is why compiler only complained about it when the parameter was 0.



#include <iostream>
#include <string>

class CppSyntaxA

public:
void f(int i = 0) const i++;
void f(const std::string&)
;

int main()

CppSyntaxA a;
a.f(1); // OK
//a.f(0); //error C2666: 'CppSyntaxA::f': 2 overloads have similar conversions
return 0;







c++ overloading ambiguous-call






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago









NathanOliver

108k19 gold badges162 silver badges241 bronze badges




108k19 gold badges162 silver badges241 bronze badges










asked 9 hours ago









Cong MaCong Ma

4636 silver badges15 bronze badges




4636 silver badges15 bronze badges







  • 2





    @NeilButterworth GCC shows the same behavior.

    – Miles Budnek
    9 hours ago











  • @Miles No, it doesn't.

    – Neil Butterworth
    9 hours ago











  • @NeilButterworth sure does: wandbox.org/permlink/mEtMFPt5KMdSVGs3 and wandbox.org/permlink/8UiG8XD7d0ol8U8v

    – Parsa
    9 hours ago












  • clang seems happy with it for what that's worth.

    – tadman
    9 hours ago











  • Obviously, I can't illustrate the lack of an error message, but GCC 8.1.0 compiles it with both -std=c++11 and -std=c++17

    – Neil Butterworth
    8 hours ago












  • 2





    @NeilButterworth GCC shows the same behavior.

    – Miles Budnek
    9 hours ago











  • @Miles No, it doesn't.

    – Neil Butterworth
    9 hours ago











  • @NeilButterworth sure does: wandbox.org/permlink/mEtMFPt5KMdSVGs3 and wandbox.org/permlink/8UiG8XD7d0ol8U8v

    – Parsa
    9 hours ago












  • clang seems happy with it for what that's worth.

    – tadman
    9 hours ago











  • Obviously, I can't illustrate the lack of an error message, but GCC 8.1.0 compiles it with both -std=c++11 and -std=c++17

    – Neil Butterworth
    8 hours ago







2




2





@NeilButterworth GCC shows the same behavior.

– Miles Budnek
9 hours ago





@NeilButterworth GCC shows the same behavior.

– Miles Budnek
9 hours ago













@Miles No, it doesn't.

– Neil Butterworth
9 hours ago





@Miles No, it doesn't.

– Neil Butterworth
9 hours ago













@NeilButterworth sure does: wandbox.org/permlink/mEtMFPt5KMdSVGs3 and wandbox.org/permlink/8UiG8XD7d0ol8U8v

– Parsa
9 hours ago






@NeilButterworth sure does: wandbox.org/permlink/mEtMFPt5KMdSVGs3 and wandbox.org/permlink/8UiG8XD7d0ol8U8v

– Parsa
9 hours ago














clang seems happy with it for what that's worth.

– tadman
9 hours ago





clang seems happy with it for what that's worth.

– tadman
9 hours ago













Obviously, I can't illustrate the lack of an error message, but GCC 8.1.0 compiles it with both -std=c++11 and -std=c++17

– Neil Butterworth
8 hours ago





Obviously, I can't illustrate the lack of an error message, but GCC 8.1.0 compiles it with both -std=c++11 and -std=c++17

– Neil Butterworth
8 hours ago












2 Answers
2






active

oldest

votes


















12














0 is special in C++. A null pointer has the value of 0 so C++ will allow the conversion of 0 to a pointer type. That means when you call



a.f(0);


You could with be calling void f(int i = 0) const with an int with the value of 0, or you could call void f(const std::string&) with a char* initialized to null.



Normally the int version would be better since it is an exact match but in this case the int version is const, so it requires "converting" a to a const CppSyntaxA, where the std::string version does not require such a conversion but does require a conversion to char* and then to std::string. This is considered enough of a change in both cases to be considered an equal conversion and thus ambiguous. Making both functions const or non const will fix the issue and the int overload will be chosen since it is better.






share|improve this answer

























  • @FrançoisAndrieux Answer updated.

    – NathanOliver
    8 hours ago











  • @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

    – Cong Ma
    8 hours ago












  • @NeilButterworth fixed.

    – NathanOliver
    8 hours ago











  • @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

    – François Andrieux
    8 hours ago







  • 1





    @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

    – NathanOliver
    8 hours ago


















4















My question is why compiler only complained about it when the parameter was 0.




Because 0 is not only an integer literal, but it is also a null pointer literal. 1 is not a null pointer literal, so there is no ambiguity.



The ambiguity arises from the implicit converting constructor of std::string that accepts a pointer to a character as an argument.



Now, the identity conversion from int to int would otherwise be preferred to the conversion from pointer to string, but there is another argument that involves a conversion: The implicit object argument. In one case, the conversion is from CppSyntaxA& to CppSyntaxA& while in other case it is CppSyntaxA& to const CppSyntaxA&.



So, one overload is preferred because of one argument, and the other overload is preferred because of another argument and thus there is no unambiguously preferred overload.




The issue will be fixed by making both functions const.




If both overloads are const qualified, then the implicit object argument conversion sequence is identical, and thus one of the overloads is unambiguously preferred.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57101872%2fcompiler-only-complains-about-the-ambiguous-overloaded-functions-when-the-parame%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    0 is special in C++. A null pointer has the value of 0 so C++ will allow the conversion of 0 to a pointer type. That means when you call



    a.f(0);


    You could with be calling void f(int i = 0) const with an int with the value of 0, or you could call void f(const std::string&) with a char* initialized to null.



    Normally the int version would be better since it is an exact match but in this case the int version is const, so it requires "converting" a to a const CppSyntaxA, where the std::string version does not require such a conversion but does require a conversion to char* and then to std::string. This is considered enough of a change in both cases to be considered an equal conversion and thus ambiguous. Making both functions const or non const will fix the issue and the int overload will be chosen since it is better.






    share|improve this answer

























    • @FrançoisAndrieux Answer updated.

      – NathanOliver
      8 hours ago











    • @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

      – Cong Ma
      8 hours ago












    • @NeilButterworth fixed.

      – NathanOliver
      8 hours ago











    • @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

      – François Andrieux
      8 hours ago







    • 1





      @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

      – NathanOliver
      8 hours ago















    12














    0 is special in C++. A null pointer has the value of 0 so C++ will allow the conversion of 0 to a pointer type. That means when you call



    a.f(0);


    You could with be calling void f(int i = 0) const with an int with the value of 0, or you could call void f(const std::string&) with a char* initialized to null.



    Normally the int version would be better since it is an exact match but in this case the int version is const, so it requires "converting" a to a const CppSyntaxA, where the std::string version does not require such a conversion but does require a conversion to char* and then to std::string. This is considered enough of a change in both cases to be considered an equal conversion and thus ambiguous. Making both functions const or non const will fix the issue and the int overload will be chosen since it is better.






    share|improve this answer

























    • @FrançoisAndrieux Answer updated.

      – NathanOliver
      8 hours ago











    • @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

      – Cong Ma
      8 hours ago












    • @NeilButterworth fixed.

      – NathanOliver
      8 hours ago











    • @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

      – François Andrieux
      8 hours ago







    • 1





      @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

      – NathanOliver
      8 hours ago













    12












    12








    12







    0 is special in C++. A null pointer has the value of 0 so C++ will allow the conversion of 0 to a pointer type. That means when you call



    a.f(0);


    You could with be calling void f(int i = 0) const with an int with the value of 0, or you could call void f(const std::string&) with a char* initialized to null.



    Normally the int version would be better since it is an exact match but in this case the int version is const, so it requires "converting" a to a const CppSyntaxA, where the std::string version does not require such a conversion but does require a conversion to char* and then to std::string. This is considered enough of a change in both cases to be considered an equal conversion and thus ambiguous. Making both functions const or non const will fix the issue and the int overload will be chosen since it is better.






    share|improve this answer















    0 is special in C++. A null pointer has the value of 0 so C++ will allow the conversion of 0 to a pointer type. That means when you call



    a.f(0);


    You could with be calling void f(int i = 0) const with an int with the value of 0, or you could call void f(const std::string&) with a char* initialized to null.



    Normally the int version would be better since it is an exact match but in this case the int version is const, so it requires "converting" a to a const CppSyntaxA, where the std::string version does not require such a conversion but does require a conversion to char* and then to std::string. This is considered enough of a change in both cases to be considered an equal conversion and thus ambiguous. Making both functions const or non const will fix the issue and the int overload will be chosen since it is better.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 8 hours ago

























    answered 8 hours ago









    NathanOliverNathanOliver

    108k19 gold badges162 silver badges241 bronze badges




    108k19 gold badges162 silver badges241 bronze badges












    • @FrançoisAndrieux Answer updated.

      – NathanOliver
      8 hours ago











    • @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

      – Cong Ma
      8 hours ago












    • @NeilButterworth fixed.

      – NathanOliver
      8 hours ago











    • @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

      – François Andrieux
      8 hours ago







    • 1





      @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

      – NathanOliver
      8 hours ago

















    • @FrançoisAndrieux Answer updated.

      – NathanOliver
      8 hours ago











    • @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

      – Cong Ma
      8 hours ago












    • @NeilButterworth fixed.

      – NathanOliver
      8 hours ago











    • @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

      – François Andrieux
      8 hours ago







    • 1





      @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

      – NathanOliver
      8 hours ago
















    @FrançoisAndrieux Answer updated.

    – NathanOliver
    8 hours ago





    @FrançoisAndrieux Answer updated.

    – NathanOliver
    8 hours ago













    @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

    – Cong Ma
    8 hours ago






    @FrançoisAndrieux I think f(int) is const and a is a non-const variable. So there is a cast from non-const to const.

    – Cong Ma
    8 hours ago














    @NeilButterworth fixed.

    – NathanOliver
    8 hours ago





    @NeilButterworth fixed.

    – NathanOliver
    8 hours ago













    @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

    – François Andrieux
    8 hours ago






    @NathanOliver Never noticed that const. Good observation and excellent answer. Edit : I guess I missed the part of the question that mentions it, must have skipped straight to the code...

    – François Andrieux
    8 hours ago





    1




    1





    @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

    – NathanOliver
    8 hours ago





    @FrançoisAndrieux Don't worry, compiler implementers do as well ;)

    – NathanOliver
    8 hours ago













    4















    My question is why compiler only complained about it when the parameter was 0.




    Because 0 is not only an integer literal, but it is also a null pointer literal. 1 is not a null pointer literal, so there is no ambiguity.



    The ambiguity arises from the implicit converting constructor of std::string that accepts a pointer to a character as an argument.



    Now, the identity conversion from int to int would otherwise be preferred to the conversion from pointer to string, but there is another argument that involves a conversion: The implicit object argument. In one case, the conversion is from CppSyntaxA& to CppSyntaxA& while in other case it is CppSyntaxA& to const CppSyntaxA&.



    So, one overload is preferred because of one argument, and the other overload is preferred because of another argument and thus there is no unambiguously preferred overload.




    The issue will be fixed by making both functions const.




    If both overloads are const qualified, then the implicit object argument conversion sequence is identical, and thus one of the overloads is unambiguously preferred.






    share|improve this answer



























      4















      My question is why compiler only complained about it when the parameter was 0.




      Because 0 is not only an integer literal, but it is also a null pointer literal. 1 is not a null pointer literal, so there is no ambiguity.



      The ambiguity arises from the implicit converting constructor of std::string that accepts a pointer to a character as an argument.



      Now, the identity conversion from int to int would otherwise be preferred to the conversion from pointer to string, but there is another argument that involves a conversion: The implicit object argument. In one case, the conversion is from CppSyntaxA& to CppSyntaxA& while in other case it is CppSyntaxA& to const CppSyntaxA&.



      So, one overload is preferred because of one argument, and the other overload is preferred because of another argument and thus there is no unambiguously preferred overload.




      The issue will be fixed by making both functions const.




      If both overloads are const qualified, then the implicit object argument conversion sequence is identical, and thus one of the overloads is unambiguously preferred.






      share|improve this answer

























        4












        4








        4








        My question is why compiler only complained about it when the parameter was 0.




        Because 0 is not only an integer literal, but it is also a null pointer literal. 1 is not a null pointer literal, so there is no ambiguity.



        The ambiguity arises from the implicit converting constructor of std::string that accepts a pointer to a character as an argument.



        Now, the identity conversion from int to int would otherwise be preferred to the conversion from pointer to string, but there is another argument that involves a conversion: The implicit object argument. In one case, the conversion is from CppSyntaxA& to CppSyntaxA& while in other case it is CppSyntaxA& to const CppSyntaxA&.



        So, one overload is preferred because of one argument, and the other overload is preferred because of another argument and thus there is no unambiguously preferred overload.




        The issue will be fixed by making both functions const.




        If both overloads are const qualified, then the implicit object argument conversion sequence is identical, and thus one of the overloads is unambiguously preferred.






        share|improve this answer














        My question is why compiler only complained about it when the parameter was 0.




        Because 0 is not only an integer literal, but it is also a null pointer literal. 1 is not a null pointer literal, so there is no ambiguity.



        The ambiguity arises from the implicit converting constructor of std::string that accepts a pointer to a character as an argument.



        Now, the identity conversion from int to int would otherwise be preferred to the conversion from pointer to string, but there is another argument that involves a conversion: The implicit object argument. In one case, the conversion is from CppSyntaxA& to CppSyntaxA& while in other case it is CppSyntaxA& to const CppSyntaxA&.



        So, one overload is preferred because of one argument, and the other overload is preferred because of another argument and thus there is no unambiguously preferred overload.




        The issue will be fixed by making both functions const.




        If both overloads are const qualified, then the implicit object argument conversion sequence is identical, and thus one of the overloads is unambiguously preferred.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        eerorikaeerorika

        99.9k6 gold badges79 silver badges153 bronze badges




        99.9k6 gold badges79 silver badges153 bronze badges



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57101872%2fcompiler-only-complains-about-the-ambiguous-overloaded-functions-when-the-parame%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

            Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу