Question

I have a data container which has following requirements:

  • Be fast: Hence templates and not normal inheritance
  • Use different implementations
  • Be able to extend those implementations with more methods
  • Data is specified via template argument and needs to be able to save pointers to data container items

The solution I have come up with is as follows:

    template<typename T, template<typename> class container_t>
class data_c
{
public:
    typedef data_c<T, container_t> value_type;
    typedef typename container_t<value_type>::ref container_ref;

    container_ref link;
    T c;
};

template<typename T>
class storage_container_impl
{
public:
    typedef T value_type;
    typedef storage_container_impl<T>* ref;
    T data;
};

template<typename _impl>
class storage_container : public _impl
{
public:
    typedef typename _impl::value_type value_type;
    typedef typename _impl::ref ref;
};

template<typename T>
using impl_storage_container = storage_container<storage_container_impl<T> >;

typedef impl_storage_container<data_c<int, impl_storage_container> > c_storage_container;

int main()
{
    c_storage_container tmp;
    tmp.data.c=5;
    tmp.data.link=&tmp;
    return tmp.data.c;
}

Which results in following error (gcc 4.7):

test1.cpp:6:48: error: no type named 'ref' in 'impl_storage_container<data_c<int, impl_storage_container> > {aka class storage_container<storage_container_impl<data_c<int, impl_storage_container> > >}'

It works if I use T *data instead of T data (But I do not want that indirection), or if I do not use storage_container_impl and have T data directly in storage_container. Using storage_container_impl mixin-style does not solve the problem as well. As container_ref is a pointer, there also should be no reason why it does not work, e.g. because there is a loop in the template declaration. This is a minimized version of the problem. Any help would be appreciated!

Was it helpful?

Solution

I know that it's not a perfect solution, but you can try to replace

typedef impl_storage_container<data_c<int, impl_storage_container> > c_storage_container;

with

typedef impl_storage_container<data_c<int, storage_container_impl> > c_storage_container;

It compiles and work. Otherwise you get endless type recursion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top