2013-10-17 33 views
1

boost::mpl::apply元函数仅适用于模板类型参数。例如,下面的工作:boost :: mpl :: apply仅适用于键入的模板参数

using namespace boost::mpl; 

template <class U, class S = int> 
struct Bar { }; 

using BarInt = apply<Bar<_1>, int>::type; 

但是,如果我有一个非类型参数单独的类模板:

template <class U, int S = 50> 
struct Quux { }; 

using QuuxInt = apply<Quux<_1>, int>::type; 

我得到一个编译错误,如:

/usr/include/boost/mpl/aux_/preprocessed/gcc/apply_wrap.hpp:36:8: error: no class template named ‘apply’ in ‘struct Quux<mpl_::arg<1> >’ 
struct apply_wrap1 
     ^
foo.cxx: In function ‘int main()’: 
foo.cxx:25:21: error: expected type-specifier 
    using QuuxInt = apply<Quux<_1>, int>::type; 
        ^

有没有办法解决这个问题,除了为Bar创建一个子类型,使所有的非类型参数都变成类型参数?

回答

1

不,没有办法绕过它。你必须写一个包装。值得庆幸的是,如果你让你的包装一元函数,它需要键入拆开包装到预期的非类型参数的参数:

template <class U, class S = std::integral_constant<int, 50>> 
struct QuuxWrap { 
    using type = Quux<U, S::value>; 
}; 

然后简单地应用在QuuxWrap得到你想要的东西:

using QuuxInt = apply<QuuxWrap<_1>, int>::type; 
// QuuxInt is Quux<int, 50> 

值为模板元编程的红头发的继子女。如果你将所有的值提升到类型,一切都变得容易处理。