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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux