<html>
<head>
<base href="http://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - link success with static template member depends on optimisation level"
href="http://llvm.org/bugs/show_bug.cgi?id=20593">20593</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>link success with static template member depends on optimisation level
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.4
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>FreeBSD
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>kamikaze@bsdforen.de
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>First of all I do not know whether static template members are permitted. I
figure there are some initialisation issues for the linker to sort out if a
template is used in multiple object files.
In any way I think the behaviour should be the same for all optimisation
levels.
EXAMPLE CODE
--- foo.hpp ---
#ifndef _FOO_HPP_
#define _FOO_HPP_
template <class T>
class foo {
private:
static T static_member;
public:
foo() {
}
static T const & static_method() {
return foo<T>::static_member;
}
T const & dynamic_method() const {
return this->static_member;
}
};
#endif /* _FOO_HPP_ */
--- main.cpp ---
#include "foo.hpp"
int main() {
foo<int> bar;
bar.dynamic_method();
foo<int>::static_method();
return 0;
}
CLANG VERSION
<span class="quote">> c++ -v</span >
FreeBSD clang version 3.4.1 (tags/RELEASE_34/dot1-final 208032) 20140512
Target: x86_64-unknown-freebsd10.0
Thread model: posix
Selected GCC installation:
OS
<span class="quote">> uname -a</span >
FreeBSD AprilRyan.norad 10.0-STABLE FreeBSD 10.0-STABLE #0 r268659: Tue Jul 15
11:25:22 CEST 2014
<a href="mailto:root@AprilRyan.norad">root@AprilRyan.norad</a>:/usr/obj/S403/amd64/usr/src/sys/S403 amd64
DIRECT COMPILE
<span class="quote">> c++ -O0 main.cpp</span >
/tmp/main-b8dc96.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
/tmp/main-b8dc96.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)
<span class="quote">> c++ -O1 main.cpp</span >
/tmp/main-17cbe6.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
/tmp/main-17cbe6.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)
<span class="quote">> c++ -O2 main.cpp
> ll</span >
total 16
-rwxr-xr-x 1 kamikaze kamikaze 7371 8 Aug 15:30 a.out*
-rw-r--r-- 1 kamikaze kamikaze 295 8 Aug 15:04 foo.hpp
-rw-r--r-- 1 kamikaze kamikaze 113 8 Aug 15:08 main.cpp
COMPILE FIRST LINK LATER
<span class="quote">> c++ -c -O0 main.cpp
> c++ -O0 main.o</span >
main.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
main.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x8):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)
<span class="quote">> c++ -c -O1 main.cpp
> c++ -O1 main.o</span >
main.o: In function `foo<int>::dynamic_method() const':
main.cpp:(.text._ZNK3fooIiE14dynamic_methodEv[_ZNK3fooIiE14dynamic_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
main.o: In function `foo<int>::static_method()':
main.cpp:(.text._ZN3fooIiE13static_methodEv[_ZN3fooIiE13static_methodEv]+0x5):
undefined reference to `foo<int>::static_member'
c++: error: linker command failed with exit code 1 (use -v to see invocation)
<span class="quote">> c++ -c -O2 main.cpp
> c++ -O2 main.o
> ll</span >
total 20
-rwxr-xr-x 1 kamikaze kamikaze 7371 8 Aug 15:35 a.out*
-rw-r--r-- 1 kamikaze kamikaze 295 8 Aug 15:04 foo.hpp
-rw-r--r-- 1 kamikaze kamikaze 113 8 Aug 15:08 main.cpp
-rw-r--r-- 1 kamikaze kamikaze 1256 8 Aug 15:35 main.o</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>