1
有一個代碼:防止模板部分特例錯誤
#include <functional>
template<typename DataType, typename Compare=std::less<DataType>>
class MyClass {
public:
explicit MyClass(const Compare& f = Compare()) {
compare = f;
};
bool foo(DataType, DataType);
private:
Compare compare;
};
template<typename DataType>
bool MyClass<DataType>::foo(DataType a, DataType b) {
return compare(a, b);
}
它同時編譯得到一個錯誤:
error: nested name specifier 'MyClass<DataType>::'
for declaration does not refer into a class, class template or class
template partial specialization bool MyClass<DataType>::foo(DataType a, DataType b) {
如何防止錯誤,聲明類以外的方法?
[演示](http://ideone.com/1ZFJnT) –