The original question has been refined.
Given a source code file named main.cpp as follows:
#include <string>
extern template class std::basic_string<char>;
template<typename T>
struct A
{
T n = {};
T get() const
{
return n;
}
};
extern template struct A<int>;
int main()
{
auto a = A<int>{}.get(); // undefined reference to `A<int>::get() const'
auto b = static_cast<int>(std::string{}.size()); // ok
return a + b;
}
What I expected:
Note that the source code has extern template class std::basic_string<char> but no template class std::basic_string<char>.
So, the compiler would not instantiate class std::basic_string<char>, then g++ main.cpp would cause link errors on line std::string{}.size() as on line A<int>{}.get().
What I observed:
It is ok to g++ main.cpp on line std::string{}.size(). Online demo
Why does extern template class technique not work as expected?
Aucun commentaire:
Enregistrer un commentaire