c++ - extern template & incomplete types -
recently when trying optimize include hierarchy stumbled upon file a.hpp
:
template<class t> class { using t = typename t::a_t; }; class b; extern template class a<b>;
which seems ill-formed. in fact seems if extern template statement @ end causes instantiation of a<b>
causes compiler complain incomplete type.
my goal have been define a<b>
in a.cpp
:
#include <b.hpp> template class a<b>;
this way avoid having include b.hpp
a.hpp
seems idea reduce compile time. not work (a.hpp
on doesn't compile!) there better way of doing this?
note: of course not use explicit template instantiation not want! "precompile" a<b>
save compilation time if used, if a<b>
not used don't want include b.hpp
in every file uses a.hpp
!
from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1448.pdf
the extern specifier used declare explicit instantiation of class template suppresses explicit instantiations of definitions of member functions , static data members not specialized in translation unit containing declaration.
thus, if definition of b required in a, cannot use extern template
without knowledge of b. of course try rid of requirement. in given case, remove using t
declaration , have meta function yield type:
template<typename t> struct get_a_t; template<typename t> struct get_a_t<a<t>> { using type = typename t::a_t; };
not sure feasible in case. needs store b
or b::a_t
, need b
. references , pointers ok, though.
Comments
Post a Comment