7

我想专门研究以下成员函数:

class foo {
    template<typename T>
    T get() const;
};

对于其他bar依赖模板的类。

例如,我想bar使用std::pair一些模板参数,例如:

template<>
std::pair<T1,T2> foo::get() const
{
    T1 x=...;
    T2 y=...;
    return std::pair<T1,T2>(x,y);
}

其中 T1 和 T2 也是模板。如何才能做到这一点?据我所知,这应该是可能的。

所以现在我可以打电话了:

some_foo.get<std::pair<int,double> >();

完整/最终答案:

template<typename T> struct traits;
class foo {
    template<typename T>
    T get() const
    {
       return traits<T>::get(*this); 
    }
};

template<typename T>
struct traits {
    static T get(foo &f)
    {
        return f.get<T>();
    }
};

template<typename T1,typename T2>
struct traits<std::pair<T1,T2> > {
        static std::pair<T1,T2> get(foo &f)
        {
                T1 x=...;
                T2 y=...;
                return std::make_pair(x,y);
        }
};
4

2 回答 2

8

您不能部分专门化函数模板,抱歉,这些是规则。您可以执行以下操作:

class foo {
   ...
};


template<typename T>
struct getter {
  static T get(const foo& some_foo);
};

template<typename T1, typename T2>
struct getter< std::pair<T1, T2> > {
static std::pair<T1, T2> get(const foo& some_foo) {
    T1 t1 = ...;
    T2 t2 = ...;
    return std::make_pair(t1, t2);
};

然后像这样称呼它

getter<std::pair<int, double> >::get(some_foo);

尽管。如果真的需要成为成员函数,您可能不得不对friend船舶或可见性进行一些处理。get

详细说明 sbi 的建议:

class foo {
   ...
   template<typename T>
   T get() const;
};

template<typename T>
T foo::get() const
{
  return getter<T>::get(*this);
  /*            ^-- specialization happens here */
}

现在你又可以说

std::pair<int,double> p = some_foo.get<std::pair<int, double> >();
于 2009-08-23T13:05:21.110 回答
1

您需要为 pair 重载成员函数,例如

template <T, V> std::pair<T, V> foo::get()

在一般情况下,您需要能够消除各种重载之间的歧义。在这种情况下,消除歧义很容易,因为 pair 在 2 种类型上模板化,而原始成员仅在 T 上模板化。

相反,如果您需要一个特化,例如 std::vector,即具有单个参数模板的容器,则必须小心,因为如果您希望实例化模板特化,编译器可能会难以理解其中模板 T 是 std::vector 或重载的特化,

template <T> std::<vector <T> foo::get() const 

您建议的语法不起作用,因为您完全专门化了成员函数,

template <>,

但是您忽略了两种未指定的类型,T1 和 T2。

于 2009-08-23T13:41:44.693 回答