Am I right?

1 ) In a switch-case statement, the expression following the word case could be a floating point literal or constant

2 ) y < x is the same as x>=y

am I correct in both of my statements?
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
so my first statement is wrong right? and also the second statement? :(
1) Wrong

2) Wrong
> 1 ) In a switch-case statement, the expression following the word case could be a floating point literal or constant

Yes. Could be a floating point literal or a floating point constexpr.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int main( int argc, char* [] )
{
    struct A
    {
        constexpr A( int aa, int bb ) : a(aa), b(bb) {}
        constexpr operator int() 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" ;
    }
}

http://coliru.stacked-crooked.com/a/083c3b5829c88037

Subject to:
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.
Topic archived. No new replies allowed.