請扔一些光對莫名其妙的一塊模板麪條:模板麪條
template <typename T, typename K> class A {
public:
T t;
K k;
template <int i, int unused = 0> struct AttributeType {
};
template <int i> AttributeType<i> getAttr();
};
template <typename T, typename K> template <int i> A<T, K>::AttributeType<i> A<T, K>::getAttr<i>() {
return t;
}
我不能拿出正確的語法定義的A::getAttr()
實施。目前的代碼失敗在GETATTR定義的行編譯:
error: function template partial specialization ‘getAttr<i>’ is not allowed
我應該如何改寫函數的定義?
作爲一個備註,而不是一個答案,如果你定義了一個模板的成員,而不是把它們拉到模板定義之外,那麼事情通常會簡單得多。也就是說,如果'getAttr'被定義在聲明的地方,它會簡單得多。 – 2011-06-03 21:03:06