Overload resolution looking into namespaces

前端 未结 2 524
眼角桃花
眼角桃花 2021-02-05 01:42

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

#include 

        
相关标签:
2条回答
  • 2021-02-05 02:09

    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.

    0 讨论(0)
  • 2021-02-05 02:15

    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.

    0 讨论(0)
提交回复
热议问题