Pregunta

Quiero definir un boost fusion :: vector en mi clase con el tamaño definido por un parámetro de plantilla. ATM Estoy haciendo esto con una especialización de una clase de ayuda, pero creo que debería haber una manera de hacerlo con Boost MPL/Fusion o algo más en una sola línea.

namespace detail
{
    template<int dim, typename T>
    struct DimensionTupleSize
    { };
    template <typename T>
    struct DimensionTupleSize<1>
    {
        enum { Dimension = 1 }
        typedef boost::fusion::vector<T> type;
    };
    template <typename T>
    struct DimensionTupleSize<2>
    {
        enum { Dimension = 2 }
        typedef boost::fusion::vector<T, T> type;
    };
    template <typename T>
    struct DimensionTupleSize<3>
    {
        enum { Dimension = 3 }
        typedef boost::fusion::vector<T, T, T> type;
    };
}

template<int Dim = 2>
class QuadTreeLevel
{
public:
    detail::DimensionTupleSize<Dim>::type tpl;
};

¿Algunas ideas?

¿Fue útil?

Solución

Puedes hacerlo de manera recursiva:

template<int N, class T> struct DimensionTupleSizeImpl
{
  typedef typename DimensionTupleSizeImpl<N-1,T>::type                   base;
  typedef typename boost::fusion::result_of::push_back<base,T>::type type;
};

template<class T> struct DimensionTupleSizeImpl<0,T>
{
  typedef boost::fusion::vector<> type;
};

template<int N, class T>
struct  DimensionTupleSize
      : boost::fusion::result_of::
        as_vector<typename DimensionTupleSizeImpl<N,T>::type>
{};

Otros consejos

Si realmente quieres una tupla en lugar de una matriz, y simplemente estás buscando más sucinto solución..,

#include <boost/array.hpp>
#include <boost/fusion/include/boost_array.hpp>
#include <boost/fusion/include/as_vector.hpp>

template<std::size_t DimN, typename T>
struct DimensionTupleSize : boost::fusion::result_of::as_vector<
    boost::array<T, DimN>
>::type
{ };

Podrías usar esto:

template<int N, typename T>
struct create_tuple
{
private:
    template<int i, int n, typename ...U>
    struct creator;

    template<typename ...U>
    struct creator<N,N, U...>
    {
        typedef boost::fusion::vector<U...> type;
    };
    template<int i, typename ...U>
    struct creator<i, N,T, U...>
    {
        typedef typename creator<i+1,N,T,U...>::type type;
    };
public:
    typedef typename creator<1,N,T>::type type;
};

template<int N, class T>
struct  DimensionTupleSize
{
    typedef typename create_tuple<N,T>::type type;
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top