Is it possible to iterate an mpl::vector at run time without instantiating the types in the vector?

前端 未结 6 1347
青春惊慌失措
青春惊慌失措 2020-12-25 14:27

Generally, I would use boost::mpl::for_each<>() to traverse a boost::mpl::vector, but this requires a functor with a template function declar

6条回答
  •  情书的邮戳
    2020-12-25 14:52

    I liked (up-voted) the solution with pointer and own defined *_for_each function. Here is an alternative using type wrapper for T if the goal is to avoid object creation till it is needed.

    template
    struct Wrapper
    {
      typedef T type;
    };
    
    struct Functor
    {
      template void operator()(T t)
      {
        T::type obj(1);
        T::type::static_fuc();
      }
    };
    
    struct T1
    {
      T1(int a) : m_a(a) { }
      int m_a;
      static inline void static_fuc() { }
    };
    struct T2
    {
      T2(int a) : m_a(a) { }
      int m_a;
      static inline void static_fuc() { }
    };
    
    void fun()
    {
      namespace mpl=boost::mpl;
      typedef mpl::vector,Wrapper > t_vec;
      mpl::for_each(Functor());
    }
    

提交回复
热议问题