3

My question is how we should use template explicit instantiation declarations in a right way?

Suppose we have some template class template<class T> Foo.

Here is my mind how to deal with this feature:

We put template declaration in Foo.h file, implementation in Foo.inl file, include this Foo.inl in the end of the Foo.h file, put extern template class Foo<int>; after #include "Foo.inl". Also we should use explicit template instantiation definition in, for example, Foo.cpp file (of course we should also inlcude Foo.h in this cpp file).

If we do all this stuff we got an error: we cannot use explicit template instantiation definition just after explicit template instantiation declaration. Ok, lets put template declaration and #include "Foo.inl" in separate file Foo.hpp and include it in Foo.cpp. Eventually we got this:

Foo.hpp

template<class T>
class Foo
{
public:
  Foo();
  ...
};

#include "Foo.inl"

Foo.inl

template<class T>
Foo<T>::Foo() {}

Foo.h

#include "Foo.hpp"
extern template class Foo<int>;

Foo.cpp

#include "Foo.hpp"
template class Foo<int>;

And use this template ike this:

test1.cpp

#include "Foo.h"
void bar()
{
  Foo f;
}

Too many Foo's do not you think? How do you deal with this?

Sorry for my english.

UPDATE:

The question is not about explicit template instantiation definitions or methods of configuring of the template code inself it is about explicit template instantiation declaration usage and about the "files hell" which is prodused if we are trying to use it.

Dmitry Katkevich
  • 883
  • 7
  • 26
  • 1
    Possible duplicate of [Template multiple definition issue](http://stackoverflow.com/questions/40810526/template-multiple-definition-issue) – Dusteh Nov 28 '16 at 09:32
  • What's the point of .h and .hpp, just ditch one? You can't get rid of the .cpp file if you want to instantiate it. You could get rid of the .inl file if you have proper documentation tools, thought it might not be a good idea. – csiz Nov 28 '16 at 09:55
  • 1
    @csiz, in this case I've create two `*.h` and `*.hpp` files cuz if I left only `*.hpp` file and put `extern template class Foo` inside it I could not include this `*.hpp` file in `*.cpp` file. You cannot use explicit instantiation definition and explicit instantiate declaration in one file (`*.cpp` in this case) – Dmitry Katkevich Nov 28 '16 at 09:58
  • Hmm, seems to just work for me http://cpp.sh/6jbe – csiz Nov 28 '16 at 10:13
  • @csiz, maybe it is VS2013 quirks. Here is error screenshot https://drive.google.com/open?id=0B8WIZg5ldEQaSkxTU0k5VVFVQU0 – Dmitry Katkevich Nov 28 '16 at 10:22
  • I copied your code into http://webcompiler.cloudapp.net/ and it also works. Sorry don't know how to help you here. – csiz Nov 28 '16 at 10:41

0 Answers0