Constexpr variable and division

前端 未结 2 1747
不知归路
不知归路 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: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;
                                  ^
    

提交回复
热议问题