繁体   English   中英

附加模板 arguments 用于模板 class 中的朋友声明?

[英]Additional template arguments for friend declaration in template class?

为了一些全球function

template<typename T, int count>
void func (const Obj<T>& obj) {
    for (int i = 0; i < count; i++)
        std::cout << obj.value << std::endl;
}

能够访问某些模板化 class 的私有字段value

template<typename T>
class Obj {
    public:
        Obj (T value);
    private:
        T value;
};

template<typename T>
Obj<T>::Obj (T value) : value(value) {}

我们需要将func<T, count>声明为Obj<T>的朋友。 但是func<T, count>必须先声明,然后才能使其成为Obj<T>的朋友,为此我们需要前向声明Obj<T> 生成的代码如下所示

// Forward declarations
template<typename T>
class Obj;

template<typename T, int count>
void func (const Obj<T>& obj);

// Obj<T>
template<typename T>
class Obj {
    public:
        Obj (T value);

        template<int count>
        friend void func<T, count> (const Obj<T>& obj);
    private:
        T value;
};

template<typename T>
Obj<T>::Obj (T value) : value(value) {} // <-- ERROR

// func<T>
template<typename T, int count>
void func (const Obj<T>& obj) {
    for (int i = 0; i < count; i++)
        std::cout << obj.value << std::endl;
}

但这使得 gcc 抱怨“在主模板声明中无效使用模板 ID 'func'”,那么我如何实际声明func<T, count>Obj<T>的每个count的朋友? 根据这个答案,我只需要用这个替换朋友声明

template<typename T1, int count>
friend void func (const Obj<T1>& obj);

据我所知,这将使func<T1, count>成为Obj<T>的朋友,无论T1T是否匹配,这是荒谬的。 是否可以将func<T, count>声明为Obj<T>的朋友而没有其他Obj<T1> (最好将func<T, count>的定义保持在Obj<T>的定义之外)

(我知道我可以让count成为一个真实的参数,但上面的例子只是我真实代码的简化。实际上我试图重载std::basic_ostream<CharT, Traits>& operator<< (std::basic_ostream<CharT, Traits>& stream, const Obj<T>& obj)用于某些 class Obj<T>以允许operator<<访问Obj<T>的私有字段。)

friend元声明必须匹配任何可能的前向声明,当然还有定义,包括模板 arguments。

这意味着你需要例如

template<typename U, int count>
friend void func(const Obj<U>& obj);

class 模板参数T和 function 模板参数U是否不同并不重要,因为无论如何都会对正确的 function 进行调用。

例子:

Obj<int> int_obj;
Obj<float> float_obj;

func<X>(int_obj);  // Will call void func<int, X>(int_obj)
func<X>(float_obj);  // Will call void func<float, X>(float_obj)

作为替代方案,您可以在 class 定义中定义function 内联,然后您不需要提供TU模板 arguments:

template<int count>
friend void func(const Obj<T>& obj)
{
    // Implementation...
}

在这两种情况下,您都应该真正拥有func的前向声明(如我的评论中所述)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM