定义可变大小的元组

发布于 2025-01-07 22:50:35 字数 897 浏览 0 评论 0原文

我想在我的类中定义一个 boost fusion::vector ,其大小由模板参数定义。 ATM 我正在​​用一个辅助类的专门化来做到这一点,但我认为应该有一种方法可以通过 boost mpl/fusion 或其他东西在一行中做到这一点。

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;
};

有什么想法吗?

I want to define a boost fusion::vector in my class with the size defined by a template parameter. ATM I'm doing this with a specialization of a helper class, but I think there should be a way to do this with boost mpl/fusion or something else in just one line.

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;
};

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

古镇旧梦 2025-01-14 22:50:35

您可以递归地执行此操作:

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>
{};

You can do it recursively :

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>
{};
七堇年 2025-01-14 22:50:35

如果您确实想要一个元组而不是数组,并且您只是在寻找最简洁的解决方案..,

#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
{ };

If you really want a tuple rather than an array, and you're simply looking for the most succinct solution..,

#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
{ };
一个人的旅程 2025-01-14 22:50:35

你可以使用这个:

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;
};

You could use this:

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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文