[LLVMbugs] [Bug 20593] link success with static template member depends on optimisation level

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Aug 11 00:24:20 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20593

Dominic Fandrey <kamikaze at bsdforen.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--- Comment #3 from Dominic Fandrey <kamikaze at bsdforen.de> ---
New code, works with -O1 and -O2, shouldn't work, because the uninitialised
static member is used.

--- foo.hpp ---
#ifndef _FOO_HPP_
#define _FOO_HPP_

#include <memory>

template <class T>
class foo {
    private:
    static std::allocator<T> alloc;

    T * const payload;

    public:
    foo(T const val) : payload(foo<T>::alloc.allocate(1)) {
        this->payload[0] = val;
    }

    virtual ~foo() {
        foo<T>::alloc.deallocate(payload, 1);
    }

    T const & getPayload() const {
        return this->payload[0];
    }
};

#endif /* _FOO_HPP_ */

--- main.cpp ---
#include "foo.hpp"
#include <iostream>

int main() {
    foo<int> bar(1337);
    std::cout << &bar.getPayload() << ": " << bar.getPayload() << '\n';
    return 0;
}


> c++ -O2 main.cpp && ./a.out                                       
0x801c06058: 1337
> c++ -O1 main.cpp && ./a.out                                       
0x801c06058: 1337
> c++ -O0 main.cpp && ./a.out                                       
/tmp/main-e745e6.o: In function `foo<int>::foo(int)':
main.cpp:(.text._ZN3fooIiEC2Ei[_ZN3fooIiEC2Ei]+0xc): undefined reference to
`foo<int>:
:alloc'
/tmp/main-e745e6.o: In function `foo<int>::~foo()':
main.cpp:(.text._ZN3fooIiED2Ev[_ZN3fooIiED2Ev]+0xc): undefined reference to
`foo<int>:
:alloc'
c++: error: linker command failed with exit code 1 (use -v to see invocation)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140811/8f6496dd/attachment.html>


More information about the llvm-bugs mailing list