Skip to content

[clang++ Bug] subobject of type 'unsigned int' is not initialized #98

Closed
@void-universe

Description

@void-universe

The following code compiled successfully with MSVC, GCC(C++11), but failed with CLANG(C++11)

#include <iostream>
#include <stdexcept>
 
int main()
{
    union Reg{
        struct {
            unsigned int A1                           :  8;
            unsigned int                                : 24;
        } bits, bitfields;
    
        unsigned int u32All;
        signed int   i32All;
        float        f32All;
    };

    constexpr uint MaxTargets = 8;

    constexpr struct
    {
        Reg a[MaxTargets];
        Reg b[MaxTargets];
        Reg c[MaxTargets];
        Reg d[MaxTargets];
    } base = {};

    std::cout << base.a[0].f32All << std::endl;
}

Error Information:

main.cpp:25:7: error: constexpr variable 'base' must be initialized by a constant expression
    } base = {};
      ^      ~~
main.cpp:25:7: note: subobject of type 'unsigned int' is not initialized
1 error generated.

Could be workaround by following modification code

#include <iostream>
#include <stdexcept>
 
int main()
{
    union Reg{
        struct {
            unsigned int  A1                           :  8;
            unsigned int  A2                              : 24;
        } bits, bitfields;
    
        unsigned int u32All;
        signed int   i32All;
        float        f32All;
    };

    constexpr uint MaxTargets = 8;

    constexpr struct
    {
        Reg a[MaxTargets];
        Reg b[MaxTargets];
        Reg c[MaxTargets];
        Reg d[MaxTargets];
    } base = {};

    std::cout << base.a[0].f32All << std::endl;
}

and

#include <iostream>
#include <stdexcept>
 
int main()
{
    union Reg{
        unsigned int u32All;
        signed int   i32All;
        float        f32All;

        struct {
            unsigned int  A1                           :  8;
            unsigned int  A2                            : 24;
        } bits, bitfields;

    };

    constexpr uint MaxTargets = 8;

    constexpr struct
    {
        Reg a[MaxTargets];
        Reg b[MaxTargets];
        Reg c[MaxTargets];
        Reg d[MaxTargets];
    } base = {};

    std::cout << base.a[0].f32All << std::endl;
}

Could someone look into this issue? The problem exists in c++11/14/17.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions