[Bug c++/85717] anonymous union in initializer list : do not handle the types correctly

daffra.claudio at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri May 11 09:13:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85717

--- Comment #4 from claudio daffra <daffra.claudio at gmail dot com> ---

I expect that the compiler regardless of the location of the types 

always gives me the same result, error message or warning

indeed :

# case integer before double :

        union 
        {
                uint64_t        integer ;                       
                double          real;
        } ;

gives me error, about narrowing conversion :

prova.cpp:32:5: error: narrowing conversion of '3.3999999999999999e+0' from
'double' to 'uint64_t {aka long long unsigned int}' inside { } [-Wnarrowing]
  t1 = { 1 ,"a",3.4 } ;
     ^

# case double before integer, compile without warning or error me :


        union 
        {
                double          real;
                uint64_t        integer ;                               
        } ;

and about output gives me undefined behavior about integer

        t1 = { 1 ,"a",3.4 } ;   
        std::cout << t1.real << "\n" ;

        t1 = { 1 ,"a",2 } ;
        std::cout << t1.integer << "\n" ;       

C:\prj>x.exe
6.6
3
3.4
4611686018427387904

however if it is a normal behavior of c++ or compiler, close it.


More information about the Gcc-bugs mailing list