Two Phase Lookup for Static variable

Viewed 50

With reference to the following code:

#include <iostream>
#include <cassert>

using std::cout;
using std::endl;

namespace tpl_ {

template<class T>
constexpr int test(T) { return 1;}

template<class T>
struct check
{
    static T const t;
    static constexpr auto val = test(T{});

    template <typename Y = T>
    void func() {
      int val = test(Y{});
      assert(val == 1);
    }
};

constexpr int test(int) {
    return 2;
}

} //tlp_

int main() {
    static_assert(tpl_::check<int>::val == 2);
}

why does the static_assert fail (and the assert() pass ?)? Wouldn't two phase lookup defer setting the value of the val (in func() as well) till the second phase where the int overload is visible since it is a dependent type?

1 Answers

Only ADL is performed from the point of instantiation. Usual unqualified lookup is performed from the point of definition.

Anything defined after the point of definition won't be considered if it isn't found via ADL specifically. Your argument here is of type int which has no associated namespaces for ADL purposes. So the second overload isn't found via ADL. The second overload is also not reachable from the point of definition of the template with the call for unqualified lookup, so it won't be found in that way either.

Related