Generic lambda vs generic function give different behaviour The Next CEO of Stack OverflowWhat 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

How to pronounce fünf in 45

Why was Sir Cadogan fired?

Early programmable calculators with RS-232

Is it correct to say moon starry nights?

Calculate the Mean mean of two numbers

Car headlights in a world without electricity

How seriously should I take size and weight limits of hand luggage?

Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?

Read/write a pipe-delimited file line by line with some simple text manipulation

Is it a bad idea to plug the other end of ESD strap to wall ground?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

"Eavesdropping" vs "Listen in on"

Avoiding the "not like other girls" trope?

What did the word "leisure" mean in late 18th Century usage?

Compensation for working overtime on Saturdays

Shortening a title without changing its meaning

Arrows in tikz Markov chain diagram overlap

Is a distribution that is normal, but highly skewed, considered Gaussian?

Find a path from s to t using as few red nodes as possible

How can the PCs determine if an item is a phylactery?

How exploitable/balanced is this homebrew spell: Spell Permanency?

Creating a script with console commands

Man transported from Alternate World into ours by a Neutrino Detector

A hang glider, sudden unexpected lift to 25,000 feet altitude, what could do this?



Generic lambda vs generic function give different behaviour



The Next CEO of Stack OverflowWhat 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










14















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



















  • 5





    Lambdas do not participate in ADL

    – Guillaume Racicot
    Mar 26 at 22:50






  • 10





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

    – chris
    Mar 26 at 22:53






  • 4





    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
    Mar 26 at 23:15












  • 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
    Mar 26 at 23:22















14















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



















  • 5





    Lambdas do not participate in ADL

    – Guillaume Racicot
    Mar 26 at 22:50






  • 10





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

    – chris
    Mar 26 at 22:53






  • 4





    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
    Mar 26 at 23:15












  • 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
    Mar 26 at 23:22













14












14








14


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 Mar 26 at 22:57







bartop

















asked Mar 26 at 22:47









bartopbartop

3,2601031




3,2601031







  • 5





    Lambdas do not participate in ADL

    – Guillaume Racicot
    Mar 26 at 22:50






  • 10





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

    – chris
    Mar 26 at 22:53






  • 4





    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
    Mar 26 at 23:15












  • 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
    Mar 26 at 23:22












  • 5





    Lambdas do not participate in ADL

    – Guillaume Racicot
    Mar 26 at 22:50






  • 10





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

    – chris
    Mar 26 at 22:53






  • 4





    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
    Mar 26 at 23:15












  • 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
    Mar 26 at 23:22







5




5





Lambdas do not participate in ADL

– Guillaume Racicot
Mar 26 at 22:50





Lambdas do not participate in ADL

– Guillaume Racicot
Mar 26 at 22:50




10




10





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

– chris
Mar 26 at 22:53





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

– chris
Mar 26 at 22:53




4




4





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
Mar 26 at 23:15






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
Mar 26 at 23:15














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
Mar 26 at 23:22





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
Mar 26 at 23:22












1 Answer
1






active

oldest

votes


















16














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
    Mar 27 at 1:09











  • Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

    – bartop
    Mar 27 at 7:05






  • 1





    @Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

    – Michael Kenzel
    Mar 27 at 10:08












  • @bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

    – Michael Kenzel
    Mar 27 at 11:31












  • @MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

    – bartop
    Mar 27 at 11:49











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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









16














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
    Mar 27 at 1:09











  • Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

    – bartop
    Mar 27 at 7:05






  • 1





    @Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

    – Michael Kenzel
    Mar 27 at 10:08












  • @bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

    – Michael Kenzel
    Mar 27 at 11:31












  • @MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

    – bartop
    Mar 27 at 11:49















16














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
    Mar 27 at 1:09











  • Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

    – bartop
    Mar 27 at 7:05






  • 1





    @Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

    – Michael Kenzel
    Mar 27 at 10:08












  • @bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

    – Michael Kenzel
    Mar 27 at 11:31












  • @MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

    – bartop
    Mar 27 at 11:49













16












16








16







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 Mar 27 at 10:06

























answered Mar 27 at 0:55









Michael KenzelMichael Kenzel

5,83811223




5,83811223







  • 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
    Mar 27 at 1:09











  • Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

    – bartop
    Mar 27 at 7:05






  • 1





    @Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

    – Michael Kenzel
    Mar 27 at 10:08












  • @bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

    – Michael Kenzel
    Mar 27 at 11:31












  • @MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

    – bartop
    Mar 27 at 11:49












  • 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
    Mar 27 at 1:09











  • Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

    – bartop
    Mar 27 at 7:05






  • 1





    @Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

    – Michael Kenzel
    Mar 27 at 10:08












  • @bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

    – Michael Kenzel
    Mar 27 at 11:31












  • @MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

    – bartop
    Mar 27 at 11:49







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
Mar 27 at 1:09





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
Mar 27 at 1:09













Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

– bartop
Mar 27 at 7:05





Seems like a right answet to me. If there is something that can be done to workaround my problem I would be thankful for comment/answer edit.

– bartop
Mar 27 at 7:05




1




1





@Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

– Michael Kenzel
Mar 27 at 10:08






@Scheff indeed, I must've mixed up the URLs somehow. Should be fixed now. Thanks for pointing that out!

– Michael Kenzel
Mar 27 at 10:08














@bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

– Michael Kenzel
Mar 27 at 11:31






@bartop can you not just remove the using directives and/or use fully-qualified names, e.g., baz::sort!?

– Michael Kenzel
Mar 27 at 11:31














@MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

– bartop
Mar 27 at 11:49





@MichaelKenzel Sure can, and I do it currently. Even though I still wonder if there is different way to avoid this ambiguity

– bartop
Mar 27 at 11:49



















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

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?