vendredi 1 juillet 2022

Two Phase Lookup for Static variable

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?

Aucun commentaire:

Enregistrer un commentaire