我有一个模板类,它有一个需要专门化的模板成员函数,如下所示:
template <typename T>
class X
{
public:
template <typename U>
void Y() {}
template <>
void Y<int>() {}
};
尽管 VS 正确处理了这个问题,但显然这不是标准的,GCC 抱怨:explicit specialization in non-namespace scope 'class X<T>'
I tried:
template <typename T>
class X
{
public:
template <typename U>
void Y() {}
};
template <typename T>
// Also tried `template<>` here
void X<T>::Y<int>() {}
但这引起了VC和GCC的抱怨。
这样做的正确方法是什么?
很常见的问题。解决这个问题的一种方法是通过重载
template <typename T>
struct type2type { typedef T type; };
template <typename T>
class X
{
public:
template <typename U>
void Y() { Y(type2type<U>()); }
private:
template<typename U>
void Y(type2type<U>) { }
void Y(type2type<int>) { }
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)