明示的実体化(インスタンス化)があります。
多くのC++解説サイトでは、この明示的実体化はCPPファイル側に記述してあります。
しかし、例えばクラスのメンバ関数ごとに実装のCPPファイルを作成する場合、
すべてのCPPファイルに明示的実体化が必要になります。
一方で明示的実体化をヘッダに記述すると、その記述のみで
コンパイル可能です。
ヘッダで明示的実体化することは、なにか問題があるのでしょうか?
よろしくお願いします。
//hoge.h
template <int a>
class hoge{
public:
int f();
int g();
};
//template class hoge<1>; これがあるとOK
//f.cpp
#include "hoge.h"
template <int a>
int hoge<a>::f(){return 1;};
template class hoge<1>; //ないとエラー
//g.cpp
#include "hoge.h"
template <int a>
int hoge<a>::g(){return 2;};
template class hoge<1>; //ないとエラー