別のテンプレートTRAITSをパラメーターとして受け入れるテンプレートクラスArrayに取り組んでいます。
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のoperator[]を特殊化したかったのですが、構文に固執しています。それが可能かどうかはわかりません。
template <typename B>
typename Array<Traits<B, RuntimeDefined>>::Struct&
Array<Traits<B, RuntimeDefined>>::operator[](size_t a_index)
{
// Access proper member differently
}
コンパイラ(g ++ 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> >’
編集。
解決策は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
}