0

Given a class template, that take too long time to compile. During developement and debugging I want to reduce compilation time by separating the defintions of member functions into separate translation units. Just for the only full specialization (this is also for the sake of compilation time reduction).

Is it possible in C++ to separate definitions of member fuctions of class template full specialization by placing them into separate TUs?

template<> void A<smth>::f() or void A<smth>::f() gives nothing in the attempt. I can't defeat link time error.

Making explicit instantiation declarations (i.e. extern template class...) of the class template visible (or invisible) (coupled with removal of void A<smth>::f()) into TUs, where member functions defined, gives nothing too.

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • You need to fully specialize the entire class, you can't fully specialize just one of the members. – super Feb 24 '20 at 12:19
  • @super Members are not template functions. So, I don't want to specialize them. – Tomilov Anatoliy Feb 24 '20 at 13:02
  • *Is it possible in C++ to separate definitions of member fuctions of class template full specialization* So what is it that you want to do then? Just declaring an extern explicit instatiation of the class template should be enough for the compiler to only create it in one TU. – super Feb 24 '20 at 13:06
  • If you just make the definition of the member functions visible from where you are explicitly instatiating them it will compile. [example](https://wandbox.org/permlink/YFX27Jo6m1rqGcD5) – super Feb 24 '20 at 13:12

1 Answers1

1

Your syntax for explicit instantiation is wrong (you declare a specialization which is not defined), it should be:

template<typename T>
void A<T>::g()
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

template void A<int>::g();
template void A<short>::g();

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302