Given the following code:
#include <iostream>
template <std::size_t N>
struct foo
{ static std::size_t value; };
template <>
std::size_t foo<0>::value = 0u;
template <size_t N>
std::size_t foo<N>::value = 1u + foo<N - 1u>::value;
int main()
{
std::cout
<< foo<3u>::value << ' '
<< foo<2u>::value << ' '
<< foo<1u>::value << ' '
<< foo<0u>::value << std::endl;
}
where the static member value
of the template struct foo
is recursively initialized, I get different outputs from g++:
3 2 1 0
and from clang++:
1 1 1 0
So seem that g++ initializes foo<N>::value
recursively using the initialized value of foo<N-1u>::value
where clang++ uses zero for foo<N-1u>::value
.
Two questions:
- is the preceding code legit or is it Undefined Behavior in some way?
- if the preceding code is legit, who's right: g++ or clang++?