This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Initialization of a static member of a template class fails



John (Eljay) Love-Jensen schrieb:
Hi Daniel,

AFAIK, all this is solved implicitly by means of vague linking (aka COMDAT/linkonce support).

As I understand it, vague linking does not solve the problem of multiple copies of inlined code, when the inline code has (what should be) a shared static data member.


But I may be mistaken.

Time to test the theory...

Hi Eljay,


Actually, it does work. Everything else would be, IMHO, a language defect; there must not be a semantic difference between a inlining / not inlining some function.

Here is the test:

M:\Temp>cat main.cpp
#include <stdio.h>
inline int& get() {
    static int i = 4711;
    return i;
}

int& getOther();

int main() {
    printf("%x == %x\n", &get(), &getOther() );
    return 0;
}

M:\Temp>cat other.cpp
inline int& get() {
    static int i = 4711;
    return i;
}
int& getOther() {
    return get();
}

M:\Temp>g++ -O1 main.cpp other.cpp -o test

M:\Temp>test
402000 == 402000

Daniel


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]