模板类的模板友好函数,它引入了新的模板参数

时间:2015-01-27 14:33:09

标签: c++ templates friend

感谢Daniel Frey对this post的回答,我知道如何使用相同的模板参数向模板类声明模板友元函数。不幸的是,使用其他模板参数声明友元函数的语法仍然让我感到厌烦。我想实现这样的目标:

template <typename T>
class Obj;

template <typename T>
Obj<T> make_obj(T t);

template <typename T, typename RetVal>
RetVal ret_obj(T t);

template <typename T>
class Obj {
private:
    T & t;
    Obj (T & t) : t(t) { }
    Obj() = delete;

    friend Obj make_obj<T>(T t);

    template <typename RetVal>
        friend RetVal ret_obj<T, RetVal>(T t);
};

template <typename T>
Obj<T> make_obj(T t) { 
    return Obj<T>(t);
}

template <typename T, typename RetVal>
RetVal ret_obj(T t) {
    return RetVal(make_obj(t).t);
}

我知道在this post中已经提出了相同的问题,但是那里接受的答案似乎不是我想要的:将参数名称更改为T2使该函数成为该对象的所有特化,而我希望T与类中的相同。

1 个答案:

答案 0 :(得分:2)

不可能让friend声明引用部分特化 - 它们指的是特定的特化或主模板。而且,功能模板无论如何都不能部分专业化 使用类模板通常无法实现功能模板的功能:

template <typename T>
struct ret_obj_helper {
    // Here goes the original definition of ret_obj - the important difference
    // is the location of the template parameter T, which is the one
    // fixed by the friend declaration below
    template <typename RetVal>
    RetVal ret_obj(T t) {return RetVal(make_obj(t).t);}
};

// I guess RetVal, having to be explicitly specified, better goes first (?)
template <typename RetVal, typename T>
RetVal ret_obj(T&& t)
{
    // Overcomplicated for the sake of perfect forwarding
    return ret_obj_helper<typename std::remove_reference<T>::type>{}.
      template ret_obj<RetVal>(std::forward<T>(t));
}

template <typename T>
class Obj {
private:
    T t;
    Obj (T t) : t(t) { }
    Obj() = delete;

    friend Obj make_obj<T>(T t);

    // Make all specializations of the member function template 
    // of ret_obj_helper<T> a friend, regardless of the return type
    template <typename RetVal>
    friend RetVal ret_obj_helper<T>::ret_obj(T t);
};

Demo