3
我正在模板類Array上接受另一個模板TRAITS作爲參數。具有模板作爲參數的類的C++成員函數特化
template <typename BASE, typename STRUCT>
class Traits {
public:
typedef BASE BaseType;
typedef STRUCT Struct;
// .. More here
};
template <class TRAITS>
class Array {
public:
typedef TRAITS Traits;
typedef typename Traits::BaseType BaseType;
typedef typename Traits::Struct Struct;
Struct& operator[](size_t i)
{
// access proper member
}
// More here...
};
我想基於Traits :: Struct專門化Array的運算符[],但是我堅持使用語法。我不確定這是否可能。
template <typename B>
typename Array<Traits<B, RuntimeDefined>>::Struct&
Array<Traits<B, RuntimeDefined>>::operator[](size_t a_index)
{
// Access proper member differently
}
編譯器(克++ 4.4)抱怨:
In file included from array.cpp:8:
array.h:346: error: invalid use of incomplete type ‘class Array<Traits<N, RuntimeDefined> >’
array.h:26: error: declaration of ‘class Array<Traits<N, isig::RuntimeDefined> >’
EDIT。
該解決方案是基於由AAA的提案,它看起來像這樣:
Struct& operator[](size_t i)
{
return OperatorAt(i, m_traits);
}
template <typename B, typename S>
inline Struct& OperatorAt(size_t i, const Traits<B, S>&)
{
// return element at i
}
template <typename B>
inline Struct& OperatorAt(size_t i, const Traits<B, RuntimeDefined>&)
{
// partial specialisation
// return element at in a different way
}
非常感謝您提供解決方案的不同觀點。 – matejk 2010-10-06 14:39:31