Gcc fails with template recursion while clang does not

Comparison of clang 3.4.2 and gcc 4.9, what is true for the following code?

#include <iostream>

template<typename T>
struct SelfRec {
    static const int value = SelfRec<T>::value;
};

int main() {
    std::cout << SelfRec<int>::value << std::endl;
    return 0;
}

      

clang prints 0, gcc gives typical template max max max error.

+3


source to share


2 answers


What is the meaning of this code? You say Clang prints 0, which isn't shocking considering it's compiled, but what does zero mean? Where did it come from?



Note that static const int value

it is not a global static variable, but exists for everyone T

. And there are infinitely many T

s, so the value should really return forever. I don't blame GCC for failing to compile it, in fact it is probably for the best.

+3


source


According to ยง 14.7.2 / 15 this behavior is undefined:

15 There is an implementation-defined number that defines limits on the total depth of recursive processes that might involve more than one template. Result of infinite recursion into an undefined instance.



So, I agree with user657267 that any compiler can be "correct". I got a response from hacker news , although I am using the standard n3337.

+2


source







All Articles