たまにやってしまうあれ
なんかおしゃれにテンプレート使っちゃうぞー、とかする時にたまにあるあれです。
どうなるんだったか、頻繁に忘れるのでメモ。
#include <iostream> using namespace std; template<class T,int n> class Array { T data[n]; public: T get(int idx); void set(T v,int idx); }; // specialization template<int n> class Array<bool,n> { bool data[n]; public: bool get(int idx); void set(bool v,int idx) { cout<<"bool"<<endl; } }; template<class T> class Array<T,0> { T data[0]; public: T get(int idx); void set(T v,int idx) { cout<<"0"<<endl; } }; int main() { // ???? Array<bool,0> ar; ar.set(true,0); }
temp_test.cpp: In function 'int main()': temp_test.cpp:36:19: error: ambiguous class template instantiation for 'class Array<bool, 0>' temp_test.cpp:15:21: error: candidates are: class Array<bool, n> temp_test.cpp:25:18: error: class Array<T, 0> temp_test.cpp:36:19: error: aggregate 'Array<bool, 0> ar' has incomplete type and cannot be defined
気持ちとしてはどっちの特定化された奴でもいいから、とりあえずどっちか使ってくれないかなーとか思うんですけど、コンパイルしてくれません。