1. How does it make sense to switch on a floating point type?
2. They are most certainly not the same, if y == x the first is false but the second is true
#include <iostream>
int main( int argc, char* [] )
{
struct A
{
constexpr A( int aa, int bb ) : a(aa), b(bb) {}
constexproperatorint() const { return a+b ; }
int a, b ;
};
constexpr A object(20,30) ;
switch( argc )
{
case 1.7f : // 1.7f implicitly converted to int at compile-time
std::cout << "1.7f\n" ; break ;
case object : // object implicitly converted to int at compile-time
std::cout << "object\n" ; break ;
default:
std::cout << "default\n" ;
}
}
Although in some contexts constant expressions must be evaluated during program translation, others may be evaluated during program execution. Since this International Standard imposes no restrictions on the accuracy of floating-point operations, it is unspecified whether the evaluation of a floating-point expression during translation yields the same result as the evaluation of the same expression (or the same operations
on the same values) during program execution.
Foot note: Nonetheless, implementations are encouraged to provide consistent results, irrespective of whether the evaluation was actually performed during translation or during program execution. - IS
> 2 ) y < x is the same as x>=y
They may or may not evaluate to the same result depending on the types of x and y. For fundamental types (ignoring the niceties when NaN is involved) , y < x is equivalent to x > y and y <= x is equivalent to x >= y.