If a class has a trivial constructor that can be completely inlined and
consists of simply initializing member variables, then static const instances
of this class can be initialized at compile time and placed into .rodata. This
would be the same behaviour that C has for static const structs and arrays.
Example:

struct A {
public:
    inline A (int v) : m_v (v) {}
private:
    int m_v;
};

int main (void)
{
    static const A s_v (42);
    return (0);
}

Compiles to (with -fno-threadsafe-statics):

.globl main
        .type   main, @function
main:
.LFB5:
        cmpb    $0, _ZGVZ4mainE3s_v
        jne     .L2
        movl    $42, _ZZ4mainE3s_v
        movb    $1, _ZGVZ4mainE3s_v
.L2:
        xorl    %eax, %eax
        ret

With the object being initialized at runtime as if it mattered. Because the
values of the member variables can not be changed after initialization, there
is no reason to do this at runtime. Because the constructor doesn't do
anything, this would not conflict with C++'s create-on-call mandate.

Because of this behaviour there is no way to create compiled-in const objects
just like creating const structs or arrays in C.


-- 
           Summary: Trivial static const objects should go to .rodata
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: msharov at hotmail dot com
 GCC build triplet: athlon-gnu-linux
  GCC host triplet: athlon-gnu-linux
GCC target triplet: athlon-gnu-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24928

Reply via email to