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

前端 未结 6 1348
青春惊慌失措
青春惊慌失措 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:50

    Just encountered the same situation and provided different solution to the problem which I would like to share. It's not as that obvious, but it uses an existing algorithm. The idea is to use pointers instead.

    typedef boost::mpl::vector container;
    
    struct functor
    {
        template void operator()(T*)
        {
            std::cout << "created " << typeid(T).name() << std::endl;
        }
    };
    
    int main()
    {
        boost::mpl::for_each(functor());
    }
    

    so here we get a null pointers, but we don't care as we are not going to use them.

    As I stated before that is not obvious in the code and would probably require some additional comments, but it still solves the question without writing any additional code.

    added

    I think Diego Sevilla suggested something similar.

提交回复
热议问题