[問題] 想請問一個enable_if 跟tempate的問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
c++11
問題(Question):
想請問一個如何把template的實作放在cpp檔, 並搭配enable_if~
如果都放在header檔的話是可以正常運作
template<class K, class V>
class aaa{
template< class T = K,
typename std::enable_if<std::is_same<T, int>::value, int>::type = 0>
RC test(K key){
cout << "this is int:" << key << endl;
};
};
http://codepad.org/RYllQky3
不過如果把implement放在cpp檔
如下:
//aaa.hpp
template<class K, class V>
class aaa{
template< class T = K,
typename std::enable_if<std::is_same<T, int>::value, int>::type = 0>
RC test(K key);
};
//aaa.cpp
template< class T = K,
typename std::enable_if<std::is_same<T, int>::value, int>::type = 0>
RC aaa<K, V>::test(K key){
cout << "this is int:" << key << endl;
};
就會出現下面的錯誤
error: ‘K’ was not declared in this scope
想請問各位前輩有遇過類似的錯誤嗎
試過了多寫法都有錯, 這只是其中一種寫法~
麻煩可以幫我解答
另外可已請問一下
如果想要將template實作放在CPP,
除了一次include hpp+cpp兩個檔之外
還有其他的方法嗎
非常謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.230.218.152
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1446528266.A.E3F.html
→
11/03 15:11, , 1F
11/03 15:11, 1F
→
11/03 15:11, , 2F
11/03 15:11, 2F
推
11/03 20:11, , 3F
11/03 20:11, 3F
推
11/04 09:17, , 4F
11/04 09:17, 4F
→
11/04 09:17, , 5F
11/04 09:17, 5F
→
11/04 11:48, , 6F
11/04 11:48, 6F
→
11/04 11:48, , 7F
11/04 11:48, 7F
→
11/04 19:58, , 8F
11/04 19:58, 8F
→
11/04 20:00, , 9F
11/04 20:00, 9F