c++ - “Partial application” 模板参数

标签 c++ templates boost-mpl

我有以下“主”模板:

template <
        template <typename> class S
    > struct TT { /*...*/ };

以及我想与 TT 一起使用的模板:

template <int N, typename T> struct S1 {};

特别是,我想用类似的东西

TT< S1<5> > t2; // "Invalid template arguments" here

它是一种对模板的局部应用。我知道 Boost.MPL 涉及这种东西。问题是我已经有了一些使用 TT 和模板的代码,例如

template <typename T> struct S2 {}; // S3, S4…

被馈送到 TT。

所以问题是:如何在对现有代码进行最小修改的情况下将 S1TT 一起使用。如果必须使用 Boost.MPL,请告诉我最合适的解决方案。

最佳答案

定义一个派生自 S1 的类模板:

template <typename T> 
struct S11 : S1<5,T>
{
};

然后使用S11,而不是S1:

TT< S11> t2;  //it is as if TT< S1<5> > t2

工作代码:http://ideone.com/y2s7n


阅读您的评论,看来您需要这个:

template<int N>
struct Magic
{
   template <typename T> 
   struct S11 : S1<N,T>
   {
   };
};

//Usage
TT<Magic<5>::S11> t2;

魔术演示:http://ideone.com/4yxvK

关于c++ - “Partial application” 模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7436182/

相关文章:

c++ - 手工自动模板(不使用C++0x)

c++ - 如何在 "two dimensional manner"中使用 boost::variant 定义异构 std::map

c++ - 使用 boost::mpl::bitor_

c++ - 将 MPL vector 转换为静态数组

c++ - 相互递归 lambda

c++ - 为什么要有指针参数?

c++ - 在 MFC CArray 中,使用不同默认模板类型的原因是什么?

C++模板参数类型推导

c++ - 向我介绍 boost::exception

c++ - 跳转到文件中位置的最快方法 (C/C++)