Bug 64293 - [C++14] explicit instantiation declaration suppresses instantiation of constexpr constructor
Summary: [C++14] explicit instantiation declaration suppresses instantiation of conste...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2014-12-12 21:29 UTC by Jonathan Wakely
Modified: 2025-11-30 01:22 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-12-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2014-12-12 21:29:53 UTC
template <typename T>
struct Foo {
  constexpr Foo()  { }

  constexpr Foo(T const& val) {
    for (auto& el : _data) {
      el = val;
    }
  }

  T _data[2];
};

extern template struct Foo<int>;

#ifdef USE
Foo<int> f = 1;
#else
template struct Foo<int>;
#endif


When compiled with -std=gnu++14 this does not instantiate the constructor:

$ g++14 -c foo.cpp && nm -C foo.o
                 U Foo<int>::Foo(int const&)
0000000000000000 W Foo<int>::Foo()
0000000000000000 W Foo<int>::Foo()

And when compiled so it uses the constructor, the explicit instantiation declaration suppresses the instantiation of that constructor:

$ g++14 -c foo.cpp -DUSE && nm -C foo.o
0000000000000038 t _GLOBAL__sub_I_f
0000000000000000 t __static_initialization_and_destruction_0(int, int)
                 U Foo<int>::Foo(int const&)
0000000000000000 B f

So neither the file that uses it nor the file that should define it has the symbol, leading to a linker error.

The symbol is defined if:
- the constructors are not constexpr, or
- the constructor is not delegated to by the default constructor, or
- the constructor does not contain a loop, or
- the explicit instantation is not declared before being defined

Oddly, clang 3.4 has almost exactly the same bug, the only difference is that the presence of the loop makes no difference, the constructor is not generated even if the body is empty. Clang 3.5 generates the constructor as I expect.