External template and incomplete types

Recently, when I was trying to optimize my include hierarchy, I came across a file a.hpp:

template<class T>
class A
{
  using t = typename T::a_t;
};

class B;

extern template class A<B>;

which seems to be poorly formed. Actually, it seems that the extern template expression at the end invokes an instance A<B>that causes the compiler to complain about an incomplete type.

My goal would be to define A<B>in a.cpp:

#include <b.hpp>
template class A<B>;

Thus, I should not include b.hppfrom a.hpp, which seems like a good idea to reduce compilation time. However, it does not work ( a.hppit does not compile by itself!) Is there a better way to do this?

. , , , ! " " A<B>, , , A<B> , b.hpp , a.hpp!

+4
3

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1448.pdf

extern, - , , .

, B, extern template B. , , . using t - :

template<typename T>
struct get_a_t;

template<typename T>
struct get_a_t<A<T>>
{
   using type = typename T::a_t;
};

, . A B B::a_t, B. .

0

extern -, , , , . , B A<B> .

extern template - , .

+1

extern template A , - . , , . ; . cpp A. , , , . :

a.hpp A.

b.cpp B .h . ( ?)

b.cpp a.hpp A; ( ).

, ,

extern A;

in your file and link the compiled b.cpp file. If you included the a.hpp file because you still need the template, you will not recompile it because you have the extern command.

0
source

Source: https://habr.com/ru/post/1623285/


All Articles