ハロー!
2 つのテンプレート タイプのうちの 1 つだけに特化したいと考えています。たとえばtemplate <typename A, typename B> class X
、単一の関数に対して特別な実装が必要X<float, sometype>::someFunc()
です。
サンプルコード:
main.h:
#include <iostream>
template <typename F, typename I>
class B
{
public:
void someFunc()
{
std::cout << "normal" << std::endl;
};
void someFuncNotSpecial()
{
std::cout << "normal" << std::endl;
};
};
template <typename I>
void B<float, I>::someFunc();
main.cpp:
#include <iostream>
#include "main.h"
using namespace std;
template <typename I>
void B<float, I>::someFunc()
{
cout << "special" << endl;
}
int main(int argc, char *argv[])
{
B<int, int> b1;
b1.someFunc();
b1.someFuncNotSpecial();
B<float, int> b2;
b2.someFunc();
b2.someFuncNotSpecial();
}
のコンパイルに失敗しましたclass B
。このように C++ でこれができないというのは本当ですか? 最善の回避策は何ですか?
[編集]
template <float, typename I>
void B<float, I>::someFunc();
main.h:26: エラー: 'float' はテンプレート定数パラメーターの有効な型ではありません
template <typename I>
void B<float, I>::someFunc();
main.h:27: エラー: 不完全な型「クラス B」の無効な使用</p>
そして、私はgccを使用しています。
[編集]
特殊化されていない関数が他にもあるため、クラス全体を特殊化したくありません。