Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有模板化typename的函数模板专门化_C++_Templates - Fatal编程技术网

C++ 具有模板化typename的函数模板专门化

C++ 具有模板化typename的函数模板专门化,c++,templates,C++,Templates,我在某个类中有一个模板方法 template<typename T> int get(T &value) const { ... } template int get(T&value)const{ ... } 还有几个专业 template<> int AAA::get<int>(int &val) const; template<> int AAA::get<AAA>(AAA &val) const

我在某个类中有一个模板方法

template<typename T> int get(T &value) const {
    ...
}
template int get(T&value)const{
...
}
还有几个专业

template<> int AAA::get<int>(int &val) const;
template<> int AAA::get<AAA>(AAA &val) const;
模板int AAA::get(int&val)const;
模板int-AAA::get(AAA&val)const;
有一个模板类型

template<const int SIZE> class BBB{
    ...
};
模板类BBB{
...
};
我需要用这种类型专门化我的模板方法

template<> int AAA::get<BBB<SIZE>>(BBB<SIZE> &val) const;
template int AAA::get(BBB&val)const;
我知道函数模板部分专门化已禁用。
但是,对于这种特殊情况,可能有一种解决方案?

您可以将其转换为模板类专门化:

class AAA
{
    template<typename T> class get_impl
    {
        public: static int get(T & value) { return(0); }
    };

    public: template<typename T> int get(T & value) const
    {
        return(get_impl<T>::get(value));
    }
};

template<> class AAA::
get_impl<int>
{
    public: static int get(int & value) { return(0); }
};

template<> class AAA::
get_impl<AAA>
{
    public: static int get(AAA & value) { return(0); }
};

template<int SIZE> class BBB{};

template<int SIZE> class AAA::
get_impl<BBB<SIZE>>
{
    public: static int get(BBB<SIZE> & value) { return(0); }
};

int main()
{
    AAA a{};
    BBB<5> b{};
    a.get(b);
}
AAA级
{
模板类get\u impl
{
public:static int get(T&value){return(0);}
};
public:template int get(T&value)const
{
返回(get_impl::get(value));
}
};
模板类AAA::
得到
{
public:staticintget(int&value){return(0);}
};
模板类AAA::
得到
{
public:static int get(AAA&value){return(0);}
};
模板类BBB{};
模板类AAA::
得到
{
public:static int get(BBB&value){return(0);}
};
int main()
{
AAA{};
BBB{};
a、 获得(b);
}

使用重载代替专门化:

int AAA::get(int &val) const;
int AAA::get(AAA &val) const;
template <int Size> int AAA::get(BBB<SIZE> &val) const;
intaaa::get(int&val)const;
intaaa::get(AAA&val)const;
模板int AAA::get(BBB&val)const;

您只需执行常规重载即可。实际上,只有当
BBB
的声明在此时可用时,重载才会起作用。