【问题标题】:Template Specialization of Template methodTemplate方法的模板特化
【发布时间】:2011-06-29 22:18:20
【问题描述】:

好的,我有:

template<typename T>
class Reader
{
    class Input
    {
        template<typename C>
        void operator()(C& val) const
        {
            /* Do Some Work */
        }
    };
};

不幸的是,“做一些工作”的通用版本对我不起作用。也不容易修改,因为它在一些繁重的模板元编程代码中间。

所以我虽然可以针对我的类型专门化该方法。所以我的第一步是尝试将通用方法从课程中拉出来。

template<typename T>
class Reader
{
    class Input
    {
        template<typename C>
        void operator()(C& val) const;
    };
};


template<typename T>
template<typename C>
void typename Reader<T>::Input template operator()<C>(C& val) const   // LINE 13
{
    /* Do Some Work */
}

不幸的是,我收到了错误:

s.h:13: error: error: '&' 标记之前的预期')'

【问题讨论】:

    标签: c++ templates specialization template-specialization


    【解决方案1】:

    照常写就行了

    template<typename T>
    template<typename C>
    void Reader<T>::Input::operator()(C& val) const   // LINE 13
    {
        /* Do Some Work */
    }
    

    虽然定义类外的通用版本并不能帮助您提供它的特殊版本,否则我确实错过了您的目标。

    【讨论】:

    • 输入不是模板类。
    • 谢谢。你是对的,我不能专门化这个方法。工作一秒钟。谢谢。
    【解决方案2】:

    我想这是不可能的,因为它算作部分函数模板专业化,这是不允许的。 void Reader&lt;T&gt;::Input::operator () (C&amp; int) 有一个 Reader&lt;T&gt;::Input * 类型的隐式第一个参数(this 指针),因此它的签名实际上是 void (Reader&lt;T&gt;::Input *, C &amp;)。您正在尝试指定C,而不是T。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2012-08-13
      • 2010-12-15
      相关资源
      最近更新 更多