gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:49:40 26 4
gpt4 key购买 nike

我有以下“主”模板:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com