【问题标题】:Getting template dependent template type in C++在 C++ 中获取模板依赖的模板类型
【发布时间】:2014-10-02 08:06:10
【问题描述】:

嗯,我正在用 C++98 编写代码并尝试使用元编程。

我写过这种“活页夹”:

template<
    template<
        template<typename TArg1_1> class TArg1, 
        template<typename TArg2_1> class TArg2, 
        template<typename TArg3_1> class TArg3, 
        typename TArg4> class TFunction,
    template<typename TArg1_1> class TArg1,
    template<typename TArg2_1> class TArg2,
    template<typename TArg3_1> class TArg3>
struct MetaBinder_4_3_templates_1_1_1_0 {
    template <typename TArg4>
    struct type : public TFunction<TArg1, TArg2, TArg3, TArg4> {};
};

我接下来要做的是使用以下代码将参数“绑定”到它:

template<
    template <typename TType> class TApplier,
    typename TInitialType>
struct PreserveQualifiers : public PreserveQualifier<
    TypeClearVolatile, 
    TypeSetVolatile, 
    typename template <typename> MetaBinder_4_3_templates_1_1_1_0 <
        PreserveQualifier,
        TypeClearConst,
        TypeSetConst,
        TApplier>::type,
    TInitialType>
{};

PreserveQualifier 声明如下:

template<
    template <typename TType> class TClearer,
    template <typename TType> class TSetter,
    template <typename TType> class TApplier,
    typename TInitialType>
struct PreserveQualifier;

但上面的代码实际上给了我这个错误(GCC 4.7,我分成几行):

error: wrong number of template arguments (3, should be 4)
error: provided for '
    template<
        template<class TType> class TClearer, 
        template<class TType> class TSetter, 
        template<class TType> class TApplier, 
        class TInitialType
    > struct PreserveQualifier'

我认为问题出在typename template &lt;typename&gt; MetaBinder_4_3_templates_1_1_1_0 &lt;...&gt;::type 构造语法中。而实际的问题是:在这种情况下我应该如何指定依赖模板类型?

【问题讨论】:

  • 在我看来,实现某事的方法过于复杂,我怀疑,相对简单。你的目标是什么?我也许可以提出一个更简单的解决方案。

标签: c++ metaprogramming template-meta-programming c++98


【解决方案1】:

试试:

MetaBinder_4_3_templates_1_1_1_0 <...>::template type

看一个简化的例子:LIVE DEMO

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2011-02-25
    相关资源
    最近更新 更多