是否有可能重复在运行时MPL :: vector的无载体实例化类型? [英] Is it possible to iterate an mpl::vector at run time without instantiating the types in the vector?
问题描述
一般情况下,我会使用的boost :: MPL :: for_each的<>()
来遍历的boost :: MPL ::矢量
,但这需要使用模板功能的仿函数声明如下所示:
Generally, I would use boost::mpl::for_each<>()
to traverse a boost::mpl::vector
, but this requires a functor with a template function declared like the following:
模板&LT; typename的T&GT; void运算符()(T&安培;){T :: staticCall();}
我这个问题是,我不希望对象T可通过的for_each&LT被实例化;&GT;
。我根本就不需要在运算符()
t参数。有没有办法做到这一点,或者到其他的for_each&LT;&GT;
不T类型的对象传递给模板函数
My problem with this is that I don't want the object T to be instantiated by for_each<>
. I don't need the T parameter in the operator()
at all. Is there a way to accomplish this, or an alternative to for_each<>
that doesn't pass an object of type T to the template function?
理想情况下,我想运营商()定义如下:
Optimally, I would like the operator() definition to look like this:
模板&LT; typename的T&GT; void运算符()(){T :: staticCall();}
当然,我不想T可在所有之前的呼叫被实例化。任何其他提示/建议也是欢迎的。
And of course, I don't want T to be instantiated at all prior to the call. Any other tips/suggestions are also welcome.
推荐答案
有趣的问题!据我所知,Boost.MPL似乎并没有提供这样的算法。然而,编写自己不应该使用迭代器是太困难了。
Interesting question! As far as I can tell, Boost.MPL does not seem to provide such an algorithm. However, writing your own should not be too difficult using iterators.
下面是一个可能的解决方案:
Here is a possible solution:
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/vector.hpp>
using namespace boost::mpl;
namespace detail {
template < typename Begin, typename End, typename F >
struct static_for_each
{
static void call( )
{
typedef typename Begin::type currentType;
F::template call< currentType >();
static_for_each< typename next< Begin >::type, End, F >::call();
}
};
template < typename End, typename F >
struct static_for_each< End, End, F >
{
static void call( )
{
}
};
} // namespace detail
template < typename Sequence, typename F >
void static_for_each( )
{
typedef typename begin< Sequence >::type begin;
typedef typename end< Sequence >::type end;
detail::static_for_each< begin, end, F >::call();
}
[命名可能不会很好选择,但远...]
[The naming may not be very well chosen, but well...]
下面是你将如何使用这个算法:
Here is how you would use this algorithm:
struct Foo
{
static void staticMemberFunction( )
{
std::cout << "Foo";
}
};
struct Bar
{
static void staticMemberFunction( )
{
std::cout << "Bar";
}
};
struct CallStaticMemberFunction
{
template < typename T >
static void call()
{
T::staticMemberFunction();
}
};
int main()
{
typedef vector< Foo, Bar > sequence;
static_for_each< sequence, CallStaticMemberFunction >(); // prints "FooBar"
}
这篇关于是否有可能重复在运行时MPL :: vector的无载体实例化类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!