Overload resolution looking into namespaces











up vote
30
down vote

favorite
5












The following code fails as expected, because no overload of get is found. Using std::getwould solve the problem.



#include <array>

int main()
{
std::array<int, 2> ar{2,3};
auto r = get<0>(ar);//fails, get was not declared in this scope
}


However, introducing a templated version of get, even though it's not matching the function call, somehow makes the compiler use the std::get version:



#include <array>

template <typename T>
void get(){};

int main()
{
std::array<int, 2> ar{2,3};

auto r = get<0>(ar);//returns 2
}


I can't find any part of the standard that explains this. Is this a bug in all 3 compilers I tested (probably not), or am I missing something?



This behaviour was tested in




  • MSVC 15.9.2

  • Clang 8.0.0

  • GCC 9.0.0 (still an experimental version)


EDIT:
I am aware of ADL. But if ADL makes the second code work, why does it not in the first part?










share|improve this question
























  • It is found by ADL. Seems that ADL is prevented without providing that declaration, .
    – felix
    Nov 20 at 13:11






  • 1




    I was suspecting ADL. However, it does not explain why ADL doesn't work without the fake-template
    – LcdDrm
    Nov 20 at 13:13










  • Alternatively, just don't give your functions names that are already within the standard library.
    – jfh
    Nov 20 at 16:26






  • 1




    @jfh Bad advice. Don’t try to work around something that namespaces already fix for you.
    – Konrad Rudolph
    Nov 20 at 16:51






  • 1




    @LcdDrm As an exercise for myself I wrote the above functionality that you describe. I can share it with you if you like.
    – jfh
    Nov 24 at 23:09















up vote
30
down vote

favorite
5












The following code fails as expected, because no overload of get is found. Using std::getwould solve the problem.



#include <array>

int main()
{
std::array<int, 2> ar{2,3};
auto r = get<0>(ar);//fails, get was not declared in this scope
}


However, introducing a templated version of get, even though it's not matching the function call, somehow makes the compiler use the std::get version:



#include <array>

template <typename T>
void get(){};

int main()
{
std::array<int, 2> ar{2,3};

auto r = get<0>(ar);//returns 2
}


I can't find any part of the standard that explains this. Is this a bug in all 3 compilers I tested (probably not), or am I missing something?



This behaviour was tested in




  • MSVC 15.9.2

  • Clang 8.0.0

  • GCC 9.0.0 (still an experimental version)


EDIT:
I am aware of ADL. But if ADL makes the second code work, why does it not in the first part?










share|improve this question
























  • It is found by ADL. Seems that ADL is prevented without providing that declaration, .
    – felix
    Nov 20 at 13:11






  • 1




    I was suspecting ADL. However, it does not explain why ADL doesn't work without the fake-template
    – LcdDrm
    Nov 20 at 13:13










  • Alternatively, just don't give your functions names that are already within the standard library.
    – jfh
    Nov 20 at 16:26






  • 1




    @jfh Bad advice. Don’t try to work around something that namespaces already fix for you.
    – Konrad Rudolph
    Nov 20 at 16:51






  • 1




    @LcdDrm As an exercise for myself I wrote the above functionality that you describe. I can share it with you if you like.
    – jfh
    Nov 24 at 23:09













up vote
30
down vote

favorite
5









up vote
30
down vote

favorite
5






5





The following code fails as expected, because no overload of get is found. Using std::getwould solve the problem.



#include <array>

int main()
{
std::array<int, 2> ar{2,3};
auto r = get<0>(ar);//fails, get was not declared in this scope
}


However, introducing a templated version of get, even though it's not matching the function call, somehow makes the compiler use the std::get version:



#include <array>

template <typename T>
void get(){};

int main()
{
std::array<int, 2> ar{2,3};

auto r = get<0>(ar);//returns 2
}


I can't find any part of the standard that explains this. Is this a bug in all 3 compilers I tested (probably not), or am I missing something?



This behaviour was tested in




  • MSVC 15.9.2

  • Clang 8.0.0

  • GCC 9.0.0 (still an experimental version)


EDIT:
I am aware of ADL. But if ADL makes the second code work, why does it not in the first part?










share|improve this question















The following code fails as expected, because no overload of get is found. Using std::getwould solve the problem.



#include <array>

int main()
{
std::array<int, 2> ar{2,3};
auto r = get<0>(ar);//fails, get was not declared in this scope
}


However, introducing a templated version of get, even though it's not matching the function call, somehow makes the compiler use the std::get version:



#include <array>

template <typename T>
void get(){};

int main()
{
std::array<int, 2> ar{2,3};

auto r = get<0>(ar);//returns 2
}


I can't find any part of the standard that explains this. Is this a bug in all 3 compilers I tested (probably not), or am I missing something?



This behaviour was tested in




  • MSVC 15.9.2

  • Clang 8.0.0

  • GCC 9.0.0 (still an experimental version)


EDIT:
I am aware of ADL. But if ADL makes the second code work, why does it not in the first part?







c++ overload-resolution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 14:03

























asked Nov 20 at 12:55









LcdDrm

542411




542411












  • It is found by ADL. Seems that ADL is prevented without providing that declaration, .
    – felix
    Nov 20 at 13:11






  • 1




    I was suspecting ADL. However, it does not explain why ADL doesn't work without the fake-template
    – LcdDrm
    Nov 20 at 13:13










  • Alternatively, just don't give your functions names that are already within the standard library.
    – jfh
    Nov 20 at 16:26






  • 1




    @jfh Bad advice. Don’t try to work around something that namespaces already fix for you.
    – Konrad Rudolph
    Nov 20 at 16:51






  • 1




    @LcdDrm As an exercise for myself I wrote the above functionality that you describe. I can share it with you if you like.
    – jfh
    Nov 24 at 23:09


















  • It is found by ADL. Seems that ADL is prevented without providing that declaration, .
    – felix
    Nov 20 at 13:11






  • 1




    I was suspecting ADL. However, it does not explain why ADL doesn't work without the fake-template
    – LcdDrm
    Nov 20 at 13:13










  • Alternatively, just don't give your functions names that are already within the standard library.
    – jfh
    Nov 20 at 16:26






  • 1




    @jfh Bad advice. Don’t try to work around something that namespaces already fix for you.
    – Konrad Rudolph
    Nov 20 at 16:51






  • 1




    @LcdDrm As an exercise for myself I wrote the above functionality that you describe. I can share it with you if you like.
    – jfh
    Nov 24 at 23:09
















It is found by ADL. Seems that ADL is prevented without providing that declaration, .
– felix
Nov 20 at 13:11




It is found by ADL. Seems that ADL is prevented without providing that declaration, .
– felix
Nov 20 at 13:11




1




1




I was suspecting ADL. However, it does not explain why ADL doesn't work without the fake-template
– LcdDrm
Nov 20 at 13:13




I was suspecting ADL. However, it does not explain why ADL doesn't work without the fake-template
– LcdDrm
Nov 20 at 13:13












Alternatively, just don't give your functions names that are already within the standard library.
– jfh
Nov 20 at 16:26




Alternatively, just don't give your functions names that are already within the standard library.
– jfh
Nov 20 at 16:26




1




1




@jfh Bad advice. Don’t try to work around something that namespaces already fix for you.
– Konrad Rudolph
Nov 20 at 16:51




@jfh Bad advice. Don’t try to work around something that namespaces already fix for you.
– Konrad Rudolph
Nov 20 at 16:51




1




1




@LcdDrm As an exercise for myself I wrote the above functionality that you describe. I can share it with you if you like.
– jfh
Nov 24 at 23:09




@LcdDrm As an exercise for myself I wrote the above functionality that you describe. I can share it with you if you like.
– jfh
Nov 24 at 23:09












2 Answers
2






active

oldest

votes

















up vote
18
down vote



accepted










ADL is not used when explicit template arguments are involved unless you introduce a template function declaration at the call point. You're using an unqualified form of get using a non-type template argument 0, so you need to introduce a template function declaration or use the qualified version of get as std::get<0>(ar).



In standardese [temp.arg.explicit]/8: (emphasis mine)




[ Note: For simple function names, argument dependent lookup (6.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (6.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.




EDIT:



As @Yakk - Adam Nevraumont has pointed out in the comment, without the presence of the template function declaration, the expression get<0>(ar) will be parsed as (get<0)>(ar), i.e as a serie of comparison expressions instead of a function call.






share|improve this answer























  • Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
    – LcdDrm
    Nov 20 at 13:34












  • That version of GCC is still under development, its too early to say that
    – Jans
    Nov 20 at 13:46






  • 5




    Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
    – Yakk - Adam Nevraumont
    Nov 20 at 15:47




















up vote
4
down vote













Note that this changed in C++20 as a result of P0846R0. An unqualified name followed by a < token for which ordinary unqualified lookup either finds one or more functions or finds nothing is now assumed to name a template and the < is parsed accordingly.






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',
    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%2f53393467%2foverload-resolution-looking-into-namespaces%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








    up vote
    18
    down vote



    accepted










    ADL is not used when explicit template arguments are involved unless you introduce a template function declaration at the call point. You're using an unqualified form of get using a non-type template argument 0, so you need to introduce a template function declaration or use the qualified version of get as std::get<0>(ar).



    In standardese [temp.arg.explicit]/8: (emphasis mine)




    [ Note: For simple function names, argument dependent lookup (6.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (6.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.




    EDIT:



    As @Yakk - Adam Nevraumont has pointed out in the comment, without the presence of the template function declaration, the expression get<0>(ar) will be parsed as (get<0)>(ar), i.e as a serie of comparison expressions instead of a function call.






    share|improve this answer























    • Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
      – LcdDrm
      Nov 20 at 13:34












    • That version of GCC is still under development, its too early to say that
      – Jans
      Nov 20 at 13:46






    • 5




      Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
      – Yakk - Adam Nevraumont
      Nov 20 at 15:47

















    up vote
    18
    down vote



    accepted










    ADL is not used when explicit template arguments are involved unless you introduce a template function declaration at the call point. You're using an unqualified form of get using a non-type template argument 0, so you need to introduce a template function declaration or use the qualified version of get as std::get<0>(ar).



    In standardese [temp.arg.explicit]/8: (emphasis mine)




    [ Note: For simple function names, argument dependent lookup (6.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (6.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.




    EDIT:



    As @Yakk - Adam Nevraumont has pointed out in the comment, without the presence of the template function declaration, the expression get<0>(ar) will be parsed as (get<0)>(ar), i.e as a serie of comparison expressions instead of a function call.






    share|improve this answer























    • Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
      – LcdDrm
      Nov 20 at 13:34












    • That version of GCC is still under development, its too early to say that
      – Jans
      Nov 20 at 13:46






    • 5




      Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
      – Yakk - Adam Nevraumont
      Nov 20 at 15:47















    up vote
    18
    down vote



    accepted







    up vote
    18
    down vote



    accepted






    ADL is not used when explicit template arguments are involved unless you introduce a template function declaration at the call point. You're using an unqualified form of get using a non-type template argument 0, so you need to introduce a template function declaration or use the qualified version of get as std::get<0>(ar).



    In standardese [temp.arg.explicit]/8: (emphasis mine)




    [ Note: For simple function names, argument dependent lookup (6.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (6.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.




    EDIT:



    As @Yakk - Adam Nevraumont has pointed out in the comment, without the presence of the template function declaration, the expression get<0>(ar) will be parsed as (get<0)>(ar), i.e as a serie of comparison expressions instead of a function call.






    share|improve this answer














    ADL is not used when explicit template arguments are involved unless you introduce a template function declaration at the call point. You're using an unqualified form of get using a non-type template argument 0, so you need to introduce a template function declaration or use the qualified version of get as std::get<0>(ar).



    In standardese [temp.arg.explicit]/8: (emphasis mine)




    [ Note: For simple function names, argument dependent lookup (6.4.2) applies even when the function name is not visible within the scope of the call. This is because the call still has the syntactic form of a function call (6.4.1). But when a function template with explicit template arguments is used, the call does not have the correct syntactic form unless there is a function template with that name visible at the point of the call. If no such name is visible, the call is not syntactically well-formed and argument-dependent lookup does not apply. If some such name is visible, argument dependent lookup applies and additional function templates may be found in other namespaces.




    EDIT:



    As @Yakk - Adam Nevraumont has pointed out in the comment, without the presence of the template function declaration, the expression get<0>(ar) will be parsed as (get<0)>(ar), i.e as a serie of comparison expressions instead of a function call.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 19:15

























    answered Nov 20 at 13:14









    Jans

    6,55412233




    6,55412233












    • Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
      – LcdDrm
      Nov 20 at 13:34












    • That version of GCC is still under development, its too early to say that
      – Jans
      Nov 20 at 13:46






    • 5




      Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
      – Yakk - Adam Nevraumont
      Nov 20 at 15:47




















    • Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
      – LcdDrm
      Nov 20 at 13:34












    • That version of GCC is still under development, its too early to say that
      – Jans
      Nov 20 at 13:46






    • 5




      Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
      – Yakk - Adam Nevraumont
      Nov 20 at 15:47


















    Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
    – LcdDrm
    Nov 20 at 13:34






    Well that makes sense. Although in GCC 9.0.0, a void get(); is enough to make ADL work again. If I interpret that snipped of the standard correctly, this is a compiler bug?
    – LcdDrm
    Nov 20 at 13:34














    That version of GCC is still under development, its too early to say that
    – Jans
    Nov 20 at 13:46




    That version of GCC is still under development, its too early to say that
    – Jans
    Nov 20 at 13:46




    5




    5




    Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
    – Yakk - Adam Nevraumont
    Nov 20 at 15:47






    Good answer, but I'd mention what get<0>(ar) is parsed as without the template: (get<0)>(ar). This makes it more clear why the existence of the template get makes a difference.
    – Yakk - Adam Nevraumont
    Nov 20 at 15:47














    up vote
    4
    down vote













    Note that this changed in C++20 as a result of P0846R0. An unqualified name followed by a < token for which ordinary unqualified lookup either finds one or more functions or finds nothing is now assumed to name a template and the < is parsed accordingly.






    share|improve this answer

























      up vote
      4
      down vote













      Note that this changed in C++20 as a result of P0846R0. An unqualified name followed by a < token for which ordinary unqualified lookup either finds one or more functions or finds nothing is now assumed to name a template and the < is parsed accordingly.






      share|improve this answer























        up vote
        4
        down vote










        up vote
        4
        down vote









        Note that this changed in C++20 as a result of P0846R0. An unqualified name followed by a < token for which ordinary unqualified lookup either finds one or more functions or finds nothing is now assumed to name a template and the < is parsed accordingly.






        share|improve this answer












        Note that this changed in C++20 as a result of P0846R0. An unqualified name followed by a < token for which ordinary unqualified lookup either finds one or more functions or finds nothing is now assumed to name a template and the < is parsed accordingly.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 18:54









        T.C.

        105k13214319




        105k13214319






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53393467%2foverload-resolution-looking-into-namespaces%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

            Сан-Квентин

            Алькесар

            Josef Freinademetz