This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/58078] New: explicitly-defaulted template class methods are not instantiated by explicit template instantiation definition
- From: "edward.hades at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sun, 04 Aug 2013 11:50:07 +0000
- Subject: [Bug c++/58078] New: explicitly-defaulted template class methods are not instantiated by explicit template instantiation definition
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58078
Bug ID: 58078
Summary: explicitly-defaulted template class methods are not
instantiated by explicit template instantiation
definition
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: edward.hades at gmail dot com
Created attachment 30606
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30606&action=edit
source code demonstrating the bug
The explicit instantiation of a class template with some explicitly-defaulted
class methods does not instantiate these methods, which leads to link errors.
Consider the code:
==> test.h <==
#include <vector>
template<class T>
class A
{
std::vector<T> x;
public:
A() = default;
A(const A&) = default;
A(A&&) = default;
A& operator=(const A&) = default;
A& operator=(A&&) = default;
};
extern template class A<double>;
==> test1.cpp <==
#include "test.h"
int main()
{
A<double> a;
A<double> b(a);
A<double> c((A<double>()));
a = b;
a = A<double>();
return 0;
}
==> test2.cpp <==
#include "test.h"
template class A<double>;
==> Makefile <==
CXX=g++
CXXFLAGS=-std=c++11 -Wall -Wextra -pedantic
test: test1.o test2.o
$(CXX) -o $@ $+
test1.o: test1.cpp test.h
test2.o: test2.cpp test.h
==> (end) <==
This code compiles fine with, for example, clang, but fails with gcc:
g++-4.8.1 -std=c++11 -Wall -Wextra -pedantic -c -o test1.o test1.cpp
g++-4.8.1 -std=c++11 -Wall -Wextra -pedantic -c -o test2.o test2.cpp
g++-4.8.1 -o test test1.o test2.o
test1.o: In function `main':
test1.cpp:(.text+0x14): undefined reference to `A<double>::A()'
test1.cpp:(.text+0x27): undefined reference to `A<double>::A(A<double> const&)'
test1.cpp:(.text+0x54): undefined reference to `A<double>::A()'
test1.cpp:(.text+0x67): undefined reference to `A<double>::operator=(A<double>
const&)'
test1.cpp:(.text+0x8b): undefined reference to `A<double>::A()'
test1.cpp:(.text+0x9e): undefined reference to
`A<double>::operator=(A<double>&&)'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
I am attaching the source codes for your convenience.