Strange GCC Behaviour

前端 未结 2 608
野性不改
野性不改 2021-02-05 09:44

Given the following C++ code:

struct vertex_type {
    float x, y, z;

    //vertex_type() {}
    //vertex_type(float x, float y, float z) : x(x), y(y), z(z) {}
         


        
相关标签:
2条回答
  • 2021-02-05 10:26

    If you have custom constructor, the compiler should call it for all vector it create. If you don't write your own, it default to a generated constructor. But as no type are complex, it just don't need to call it. And the array is store as a constant table in the binary.

    Try inlining your default constructor and let it empty. Of course, it may only work wit h optimization enabled

    0 讨论(0)
  • 2021-02-05 10:33

    This is a long-standing missing optimization in GCC. It should be able to generate the same code for both cases, but it can't.

    Without the constructors, your vertex_type is a POD structure, which GCC can initialize static/global instances of at compile time. With the constructors, the best it can do is generate code to initialize the global at program startup.

    0 讨论(0)
提交回复
热议问题