I am working on an Array template array that accepts another TRAITS template as a parameter.
template <typename BASE, typename STRUCT>
class Traits {
public:
typedef BASE BaseType;
typedef STRUCT Struct;
};
template <class TRAITS>
class Array {
public:
typedef TRAITS Traits;
typedef typename Traits::BaseType BaseType;
typedef typename Traits::Struct Struct;
Struct& operator[](size_t i)
{
}
};
I would like to specialize the [] Array operator based on Traits :: Struct, however I am stuck in the syntax. I'm not sure if it is possible at all.
template <typename B>
typename Array<Traits<B, RuntimeDefined>>::Struct&
Array<Traits<B, RuntimeDefined>>::operator[](size_t a_index)
{
// Access proper member differently
}
The compiler (g ++ 4.4) complains:
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.
The solution is based on the aaa proposal and is as follows:
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>&)
{
}
template <typename B>
inline Struct& OperatorAt(size_t i, const Traits<B, RuntimeDefined>&)
{
}
source
share