0
template <uint_fast64_t MAX_RAND>
    struct RandomLimit
    {
        static const uint_fast64_t steps =
            RAND_MAX >= MAX_RAND ? 
            1 : 
            RandomLimit<MAX_RAND / (RAND_MAX + 1)>::steps + 1;
    };

The code above yields and error; steps is undefined. At run time, if a function call would be in place of the last expression, this function would not be called and no side - effects would happen. Why isn't this code eliminated when the condition holds true?

Note that RAND_MAX is the cstdlib constant and I'm using a Visual Studio 2015 setup.

3

1 Answer 1

1

Probably yours is a linker error, not a compiler error.

That is, the steps member is properly declared in a header file, so the code compiles fine, but then, if the condition is always true, then the compiler optimizes out the else expression and no reference to the steps variable is emitted. However, if the condition is not constant, then both branches are compiled, and you get an undefined reference error.

The solution, obviously, is to add the proper library to the linker command.

2
  • @MciprianM: Ahhh, that's why you should always copy the full error message! fatal error: template instantiation depth exceeds maximum of 900, that's because you have a recursive template instantiation and exeed the maximum of 900 iterations!!!
    – rodrigo
    Mar 31, 2017 at 18:52
  • actually in visual studio I got error C2065: 'steps': undeclared identifier so my question holds. But after all this I think I got my answer. The expression after ':' has to be compilable and it is not!
    – MciprianM
    Mar 31, 2017 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.