Could you explain to me what is actually happening in my app? The outcome of my program is "0 1 1" when I expect "0 1 2" and so on...
#include <iostream>
template<int N>
struct counter {
friend constexpr int get(counter<N>);
};
template<int N>
struct writer {
friend constexpr int get(counter<N>) {
return N;
}
};
template<int N, bool B = noexcept(get(counter<N + 1>()))>
struct GetMaxCounter
{
static constexpr int max = GetMaxCounter<N + 1>::max;
};
template<int N>
struct GetMaxCounter<N, false>
{
static constexpr int max = N;
};
int main()
{
std::cout << GetMaxCounter<0>::max << std::endl;
writer<1>();
std::cout << GetMaxCounter<0>::max << std::endl;
writer<2>();
std::cout << GetMaxCounter<0>::max << std::endl;
}
What happens when I call GetMaxCounter<0>::max for the third time? Shouldn't template argument B be reevaluated to "true"?