Constexpr variable and division

前端 未结 2 1743
不知归路
不知归路 2021-01-22 16:07

I\'m trying to evaluate this simple expression at compile time using C++11 new constexpr feature:

template 
class Test
{
   static constexpr          


        
相关标签:
2条回答
  • 2021-01-22 16:56

    Solved. One of the template instance had b=0. Somehow, Clang didn't warn me I was dividing by zero. And +Inf is not a constant expression.

    0 讨论(0)
  • 2021-01-22 16:59

    The reason why:

    Test<10,0> test1 ;
    

    fails is because you have undefined behavior due to division by zero. This is covered in the draft C++ standard section 5.6 [expr.mul] which says:

    If the second operand of / or % is zero the behavior is undefined

    and constant expressions specifically exclude undefined behavior. I am not sure what version of clang you are using but the versions I have available online do provide a divide by zero warning (see it live):

    note: division by zero
    static constexpr double c = a / b;
                                  ^
    
    0 讨论(0)
提交回复
热议问题