vendredi 31 mars 2017

Why does the ternary operator work differently at compile time compared to the run time?

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.

Aucun commentaire:

Enregistrer un commentaire