【问题标题】:How To Forward Declare Template instance member function如何转发声明模板实例成员函数
【发布时间】:2019-02-22 20:10:54
【问题描述】:

带有模板成员函数的CRTP接口,调用有问题 实现模板成员函数,由于没有前向声明 的模板成员函数。

IF 类是接口,Src 类是实现。

    template<class T_Src>
    struct IF {


        template<class T>
        static void f1()
        {
            T_Src::template imp_f1<T>();
        }

template<class T>
    inline void f3()
    {       
        /*this line does not compile*/
        static_cast<T_Src*>(this)->imp_f3<T>();
    }
}


struct Src:public IF<Src>
{
    template<class T>
    static void imp_f1()
    {

    }



    template<class T>
    inline void imp_f3()
    {

    }

};

在 IF::f1 中的静态函数接口实现工作正常

但是对于 IF::f3 我得到 MSVC 错误

" C2760: 语法错误:意外的标记 ')',预期的 '表达式"

总而言之,这适用于全局和静态成员模板函数,但对于类成员模板函数似乎需要前向声明。

不要介意解决方案,包括使用一些额外的模板魔法来解决这个问题

enter link description here

但是,该修复对我没有任何作用。

目前试图避免我的 CRTP 界面因无法在界面中使用实例模板功能而受到限制。

干杯

【问题讨论】:

  • 近距离投票很奇怪...
  • 2个sn-p的区别不仅仅是静态调用,还有template关键字缺失..

标签: c++ templates c++17


【解决方案1】:

您必须在imp_f3&lt;T&gt;() 之前添加template 关键字,就像您为T_Src::template imp_f1&lt;T&gt;() 所做的那样:

static_cast<T_Src*>(this)->template imp_f3<T>();

请查看this question 以获得解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多