Generic lambda vs generic function give different behaviourWhat is a lambda (function)?What is the difference...

If you attempt to grapple an opponent that you are hidden from, do they roll at disadvantage?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

What would be the benefits of having both a state and local currencies?

voltage of sounds of mp3files

How do I define a right arrow with bar in LaTeX?

Implement the Thanos sorting algorithm

Do I need a multiple entry visa for a trip UK -> Sweden -> UK?

Is a roofing delivery truck likely to crack my driveway slab?

Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past

How was Earth single-handedly capable of creating 3 of the 4 gods of chaos?

Teaching indefinite integrals that require special-casing

Go Pregnant or Go Home

Can a monster with multiattack use this ability if they are missing a limb?

Using parameter substitution on a Bash array

Where in the Bible does the greeting ("Dominus Vobiscum") used at Mass come from?

when is out of tune ok?

How can I replace every global instance of "x[2]" with "x_2"

Why Were Madagascar and New Zealand Discovered So Late?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Mapping a list into a phase plot

Print name if parameter passed to function

Why does John Bercow say “unlock” after reading out the results of a vote?

Finding all intervals that match predicate in vector

Is there an Impartial Brexit Deal comparison site?



Generic lambda vs generic function give different behaviour


What is a lambda (function)?What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What is the difference between a 'closure' and a 'lambda'?Why are Python lambdas useful?Distinct() with lambda?list comprehension vs. lambda + filterWhat is a lambda expression in C++11?Calling `this` member function from generic lambda - clang vs gccConstructing std::function argument from lambda













9















Take following code as an example



#include <algorithm>

namespace baz {
template<class T>
void sort(T&&){}
}

namespace boot {
const auto sort = [](auto &&){};
}

void foo (){
using namespace std;
using namespace baz;
sort(1);
}

void bar(){
using namespace std;
using namespace boot;
sort(1);
}


I expected that since foo compiled, then bar shall compile as well. To my surprise, the foo compiles correctly and bar has problem with ambiguous call to sort function. Am I doing something illegal here or this is proper way compiler should behave? If so, why is it so different. I though generic lambda can be treated as syntactic sugar for generic function.



live example










share|improve this question




















  • 4





    Lambdas do not participate in ADL

    – Guillaume Racicot
    3 hours ago






  • 5





    This isn't ADL. An int argument doesn't come from any namespace.

    – chris
    3 hours ago






  • 2





    Should this really be ambiguous, though? std::sort() doesn't take 1 parameter as input, it takes at least 2, so why is the compiler even considering it as a candidate for a call that passes only 1 parameter value?

    – Remy Lebeau
    2 hours ago













  • There must be something about the extra layer of indirection that the lambda introduces. With the first example, the call is made to ::baz::sort, but in the second example, it would have to find ::boot::mystery_lambda_type::operator(). That extra step might be what causes std::sort to be considered first. I don't have the standard in front of me so can't be sure about this.

    – alter igel
    2 hours ago
















9















Take following code as an example



#include <algorithm>

namespace baz {
template<class T>
void sort(T&&){}
}

namespace boot {
const auto sort = [](auto &&){};
}

void foo (){
using namespace std;
using namespace baz;
sort(1);
}

void bar(){
using namespace std;
using namespace boot;
sort(1);
}


I expected that since foo compiled, then bar shall compile as well. To my surprise, the foo compiles correctly and bar has problem with ambiguous call to sort function. Am I doing something illegal here or this is proper way compiler should behave? If so, why is it so different. I though generic lambda can be treated as syntactic sugar for generic function.



live example










share|improve this question




















  • 4





    Lambdas do not participate in ADL

    – Guillaume Racicot
    3 hours ago






  • 5





    This isn't ADL. An int argument doesn't come from any namespace.

    – chris
    3 hours ago






  • 2





    Should this really be ambiguous, though? std::sort() doesn't take 1 parameter as input, it takes at least 2, so why is the compiler even considering it as a candidate for a call that passes only 1 parameter value?

    – Remy Lebeau
    2 hours ago













  • There must be something about the extra layer of indirection that the lambda introduces. With the first example, the call is made to ::baz::sort, but in the second example, it would have to find ::boot::mystery_lambda_type::operator(). That extra step might be what causes std::sort to be considered first. I don't have the standard in front of me so can't be sure about this.

    – alter igel
    2 hours ago














9












9








9


1






Take following code as an example



#include <algorithm>

namespace baz {
template<class T>
void sort(T&&){}
}

namespace boot {
const auto sort = [](auto &&){};
}

void foo (){
using namespace std;
using namespace baz;
sort(1);
}

void bar(){
using namespace std;
using namespace boot;
sort(1);
}


I expected that since foo compiled, then bar shall compile as well. To my surprise, the foo compiles correctly and bar has problem with ambiguous call to sort function. Am I doing something illegal here or this is proper way compiler should behave? If so, why is it so different. I though generic lambda can be treated as syntactic sugar for generic function.



live example










share|improve this question
















Take following code as an example



#include <algorithm>

namespace baz {
template<class T>
void sort(T&&){}
}

namespace boot {
const auto sort = [](auto &&){};
}

void foo (){
using namespace std;
using namespace baz;
sort(1);
}

void bar(){
using namespace std;
using namespace boot;
sort(1);
}


I expected that since foo compiled, then bar shall compile as well. To my surprise, the foo compiles correctly and bar has problem with ambiguous call to sort function. Am I doing something illegal here or this is proper way compiler should behave? If so, why is it so different. I though generic lambda can be treated as syntactic sugar for generic function.



live example







c++ lambda c++14






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago







bartop

















asked 3 hours ago









bartopbartop

3,2331030




3,2331030








  • 4





    Lambdas do not participate in ADL

    – Guillaume Racicot
    3 hours ago






  • 5





    This isn't ADL. An int argument doesn't come from any namespace.

    – chris
    3 hours ago






  • 2





    Should this really be ambiguous, though? std::sort() doesn't take 1 parameter as input, it takes at least 2, so why is the compiler even considering it as a candidate for a call that passes only 1 parameter value?

    – Remy Lebeau
    2 hours ago













  • There must be something about the extra layer of indirection that the lambda introduces. With the first example, the call is made to ::baz::sort, but in the second example, it would have to find ::boot::mystery_lambda_type::operator(). That extra step might be what causes std::sort to be considered first. I don't have the standard in front of me so can't be sure about this.

    – alter igel
    2 hours ago














  • 4





    Lambdas do not participate in ADL

    – Guillaume Racicot
    3 hours ago






  • 5





    This isn't ADL. An int argument doesn't come from any namespace.

    – chris
    3 hours ago






  • 2





    Should this really be ambiguous, though? std::sort() doesn't take 1 parameter as input, it takes at least 2, so why is the compiler even considering it as a candidate for a call that passes only 1 parameter value?

    – Remy Lebeau
    2 hours ago













  • There must be something about the extra layer of indirection that the lambda introduces. With the first example, the call is made to ::baz::sort, but in the second example, it would have to find ::boot::mystery_lambda_type::operator(). That extra step might be what causes std::sort to be considered first. I don't have the standard in front of me so can't be sure about this.

    – alter igel
    2 hours ago








4




4





Lambdas do not participate in ADL

– Guillaume Racicot
3 hours ago





Lambdas do not participate in ADL

– Guillaume Racicot
3 hours ago




5




5





This isn't ADL. An int argument doesn't come from any namespace.

– chris
3 hours ago





This isn't ADL. An int argument doesn't come from any namespace.

– chris
3 hours ago




2




2





Should this really be ambiguous, though? std::sort() doesn't take 1 parameter as input, it takes at least 2, so why is the compiler even considering it as a candidate for a call that passes only 1 parameter value?

– Remy Lebeau
2 hours ago







Should this really be ambiguous, though? std::sort() doesn't take 1 parameter as input, it takes at least 2, so why is the compiler even considering it as a candidate for a call that passes only 1 parameter value?

– Remy Lebeau
2 hours ago















There must be something about the extra layer of indirection that the lambda introduces. With the first example, the call is made to ::baz::sort, but in the second example, it would have to find ::boot::mystery_lambda_type::operator(). That extra step might be what causes std::sort to be considered first. I don't have the standard in front of me so can't be sure about this.

– alter igel
2 hours ago





There must be something about the extra layer of indirection that the lambda introduces. With the first example, the call is made to ::baz::sort, but in the second example, it would have to find ::boot::mystery_lambda_type::operator(). That extra step might be what causes std::sort to be considered first. I don't have the standard in front of me so can't be sure about this.

– alter igel
2 hours ago












2 Answers
2






active

oldest

votes


















4














The problem here is not that the call to sort is ambiguous, but that the name sort is ambiguous. Name lookup happens before overload resolution.



I believe the relevant section is [basic.lookup]/1, specifically




[…] The declarations found by name lookup shall either all denote the same entity or shall all denote functions or function templates; in the latter case, the declarations are said to form a set of overloaded functions ([over.load]). […]




In your case, the name sort denotes both, the object boot::sort as well as the set of overloaded functions std::sort. Therefore, name lookup fails.



Your code is really no different from if you had written, for example



namespace baz {
int a;
}

namespace boot {
int a;
}

void foo() {
using namespace baz;
using namespace boot;
a = 42; // error: reference to 'a' is ambiguous
}


Try it out here; compare this to a case that actually has an ambiguous function call; note how the error message is the same as in your case, specifically referring to the name itself being ambiguous rather than the function call.






share|improve this answer





















  • 1





    I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

    – Mike
    48 mins ago











  • @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

    – Michael Kenzel
    25 mins ago



















0














I used the function typeid to watch how compiler identify lambda, function, function pointer, std::function, and each in them is different from any other.



I have not clarified why compiler work in this way, but obviously, behavior compiled between lambda and function are different.lambda must check it has the authority to use or change which value declared in this namespace when compiling, so the range compiled is limited, situation that can use all just like a function is precious few. At least there is essential distinction between them.



Consider about when lambda is used anonymously, compiler may not have to remember the initial address of lambda, compare lambda is impossible.I thought that's one of reasons.



And there should be more practical reasons for compiler has no support for distinguish named lambda from function by parameter list or return type, if it appeared in future, it won't be named overload probably.






share|improve this answer










New contributor




LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    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%2f55367269%2fgeneric-lambda-vs-generic-function-give-different-behaviour%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









    4














    The problem here is not that the call to sort is ambiguous, but that the name sort is ambiguous. Name lookup happens before overload resolution.



    I believe the relevant section is [basic.lookup]/1, specifically




    […] The declarations found by name lookup shall either all denote the same entity or shall all denote functions or function templates; in the latter case, the declarations are said to form a set of overloaded functions ([over.load]). […]




    In your case, the name sort denotes both, the object boot::sort as well as the set of overloaded functions std::sort. Therefore, name lookup fails.



    Your code is really no different from if you had written, for example



    namespace baz {
    int a;
    }

    namespace boot {
    int a;
    }

    void foo() {
    using namespace baz;
    using namespace boot;
    a = 42; // error: reference to 'a' is ambiguous
    }


    Try it out here; compare this to a case that actually has an ambiguous function call; note how the error message is the same as in your case, specifically referring to the name itself being ambiguous rather than the function call.






    share|improve this answer





















    • 1





      I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

      – Mike
      48 mins ago











    • @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

      – Michael Kenzel
      25 mins ago
















    4














    The problem here is not that the call to sort is ambiguous, but that the name sort is ambiguous. Name lookup happens before overload resolution.



    I believe the relevant section is [basic.lookup]/1, specifically




    […] The declarations found by name lookup shall either all denote the same entity or shall all denote functions or function templates; in the latter case, the declarations are said to form a set of overloaded functions ([over.load]). […]




    In your case, the name sort denotes both, the object boot::sort as well as the set of overloaded functions std::sort. Therefore, name lookup fails.



    Your code is really no different from if you had written, for example



    namespace baz {
    int a;
    }

    namespace boot {
    int a;
    }

    void foo() {
    using namespace baz;
    using namespace boot;
    a = 42; // error: reference to 'a' is ambiguous
    }


    Try it out here; compare this to a case that actually has an ambiguous function call; note how the error message is the same as in your case, specifically referring to the name itself being ambiguous rather than the function call.






    share|improve this answer





















    • 1





      I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

      – Mike
      48 mins ago











    • @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

      – Michael Kenzel
      25 mins ago














    4












    4








    4







    The problem here is not that the call to sort is ambiguous, but that the name sort is ambiguous. Name lookup happens before overload resolution.



    I believe the relevant section is [basic.lookup]/1, specifically




    […] The declarations found by name lookup shall either all denote the same entity or shall all denote functions or function templates; in the latter case, the declarations are said to form a set of overloaded functions ([over.load]). […]




    In your case, the name sort denotes both, the object boot::sort as well as the set of overloaded functions std::sort. Therefore, name lookup fails.



    Your code is really no different from if you had written, for example



    namespace baz {
    int a;
    }

    namespace boot {
    int a;
    }

    void foo() {
    using namespace baz;
    using namespace boot;
    a = 42; // error: reference to 'a' is ambiguous
    }


    Try it out here; compare this to a case that actually has an ambiguous function call; note how the error message is the same as in your case, specifically referring to the name itself being ambiguous rather than the function call.






    share|improve this answer















    The problem here is not that the call to sort is ambiguous, but that the name sort is ambiguous. Name lookup happens before overload resolution.



    I believe the relevant section is [basic.lookup]/1, specifically




    […] The declarations found by name lookup shall either all denote the same entity or shall all denote functions or function templates; in the latter case, the declarations are said to form a set of overloaded functions ([over.load]). […]




    In your case, the name sort denotes both, the object boot::sort as well as the set of overloaded functions std::sort. Therefore, name lookup fails.



    Your code is really no different from if you had written, for example



    namespace baz {
    int a;
    }

    namespace boot {
    int a;
    }

    void foo() {
    using namespace baz;
    using namespace boot;
    a = 42; // error: reference to 'a' is ambiguous
    }


    Try it out here; compare this to a case that actually has an ambiguous function call; note how the error message is the same as in your case, specifically referring to the name itself being ambiguous rather than the function call.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 47 mins ago

























    answered 1 hour ago









    Michael KenzelMichael Kenzel

    5,06811020




    5,06811020








    • 1





      I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

      – Mike
      48 mins ago











    • @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

      – Michael Kenzel
      25 mins ago














    • 1





      I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

      – Mike
      48 mins ago











    • @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

      – Michael Kenzel
      25 mins ago








    1




    1





    I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

    – Mike
    48 mins ago





    I think this is actually the right answer. And I would like to add that if both the template function sort and the lambda sort were declared in the same namespace, it would be an error. You cannot have a function and non-function with the same name in the same namespace. So there could never be an overload set that has both true functions and function-like objects.

    – Mike
    48 mins ago













    @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

    – Michael Kenzel
    25 mins ago





    @Mike Kudos to you, Sir, for deleting your answer despite putting quite some effort into it and getting that many upvotes!

    – Michael Kenzel
    25 mins ago













    0














    I used the function typeid to watch how compiler identify lambda, function, function pointer, std::function, and each in them is different from any other.



    I have not clarified why compiler work in this way, but obviously, behavior compiled between lambda and function are different.lambda must check it has the authority to use or change which value declared in this namespace when compiling, so the range compiled is limited, situation that can use all just like a function is precious few. At least there is essential distinction between them.



    Consider about when lambda is used anonymously, compiler may not have to remember the initial address of lambda, compare lambda is impossible.I thought that's one of reasons.



    And there should be more practical reasons for compiler has no support for distinguish named lambda from function by parameter list or return type, if it appeared in future, it won't be named overload probably.






    share|improve this answer










    New contributor




    LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      I used the function typeid to watch how compiler identify lambda, function, function pointer, std::function, and each in them is different from any other.



      I have not clarified why compiler work in this way, but obviously, behavior compiled between lambda and function are different.lambda must check it has the authority to use or change which value declared in this namespace when compiling, so the range compiled is limited, situation that can use all just like a function is precious few. At least there is essential distinction between them.



      Consider about when lambda is used anonymously, compiler may not have to remember the initial address of lambda, compare lambda is impossible.I thought that's one of reasons.



      And there should be more practical reasons for compiler has no support for distinguish named lambda from function by parameter list or return type, if it appeared in future, it won't be named overload probably.






      share|improve this answer










      New contributor




      LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        I used the function typeid to watch how compiler identify lambda, function, function pointer, std::function, and each in them is different from any other.



        I have not clarified why compiler work in this way, but obviously, behavior compiled between lambda and function are different.lambda must check it has the authority to use or change which value declared in this namespace when compiling, so the range compiled is limited, situation that can use all just like a function is precious few. At least there is essential distinction between them.



        Consider about when lambda is used anonymously, compiler may not have to remember the initial address of lambda, compare lambda is impossible.I thought that's one of reasons.



        And there should be more practical reasons for compiler has no support for distinguish named lambda from function by parameter list or return type, if it appeared in future, it won't be named overload probably.






        share|improve this answer










        New contributor




        LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        I used the function typeid to watch how compiler identify lambda, function, function pointer, std::function, and each in them is different from any other.



        I have not clarified why compiler work in this way, but obviously, behavior compiled between lambda and function are different.lambda must check it has the authority to use or change which value declared in this namespace when compiling, so the range compiled is limited, situation that can use all just like a function is precious few. At least there is essential distinction between them.



        Consider about when lambda is used anonymously, compiler may not have to remember the initial address of lambda, compare lambda is impossible.I thought that's one of reasons.



        And there should be more practical reasons for compiler has no support for distinguish named lambda from function by parameter list or return type, if it appeared in future, it won't be named overload probably.







        share|improve this answer










        New contributor




        LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited 12 mins ago





















        New contributor




        LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 20 mins ago









        LuLiLuLi

        233




        233




        New contributor




        LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        LuLi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            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%2f55367269%2fgeneric-lambda-vs-generic-function-give-different-behaviour%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

            Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...

            Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

            VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...