如何正确地声明一个模板类的嵌套类的朋友? [英] How to properly declare a friend of a nested class of a template class?

查看:285
本文介绍了如何正确地声明一个模板类的嵌套类的朋友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下操作时:

 模板< typename T& 
class Container
{
public:
class Iterator
{
friend bool operator ==(const Iterator& x,const Iterator& y);
};
};

gcc给我以下警告和建议:

 警告:朋友声明
'bool operator ==(const Container< T> :: Iterator&
const Container< T> :: Iterator& '
声明一个非模板函数[-Wnon-template-friend]

friend bool operator ==(const Iterator& x,const Iterator& y)
^

(如果这不是你想要的,
确保函数模板已经声明为
,并在函数名后添加<> )



我相当肯定这是一个新的警告,因为我一直这样做,没有任何问题。



有人可以解释为什么这是一个警告,它警告什么?

解决方案

这是一个事实,这实际上是不可能定义运算符== out-of-class。 / p>

也就是说, friend 声明与非模板 / code>函数 - 例如 Container< Int> :: Iterator 作为朋友的函数

  bool operator ==(const Container< Int> :: Iterator& const Container< Int> :: Iterator& 

此函数不是模板,因此几乎没有办法为类模板定义之外的所有可能的容器定义 operator ==



如果你尝试做

 模板< class T& 
bool operator ==(const Container< T> :: Iterator& const Container< T> :: Iterator&);

这是一个函数模板,不匹配好友声明。 (在这种情况下,它更糟糕,因为你不能实际使用这个运算符,因为 T 是在一个非推导的上下文。)



警告消息建议一个可能的修复 - 首先声明一个函数模板,然后结束它的特殊化。 (您需要将类中的 Iterator 拖到自己单独的类模板中,以便可以推导出 T 。)另一个可能的修正是在类模板定义中定义函数。


When I do the following:

template <typename T>
class Container
{
public:
    class Iterator
    {
        friend bool operator==(const Iterator& x, const Iterator& y);
    };
};

gcc gives me the following warning and suggestion:

warning: friend declaration 
'bool operator==(const Container<T>::Iterator&, 
                 const Container<T>::Iterator&)' 
declares a non-template function [-Wnon-template-friend]

friend bool operator==(const Iterator& x, const Iterator& y);
                                                           ^

(if this is not what you intended, 
 make sure the function template has already been declared 
 and add <> after the function name here)

I am fairly sure that this is a new warning, since I have always done it like this and never had any problems.

Can someone please explain why is this a warning, and what it warns about?

解决方案

It's warning about the fact that it is going to be virtually impossible to define that operator== out-of-class.

That is to say, that friend declaration befriends a non-template operator== function - for example, Container<Int>::Iterator has as a friend the function

bool operator==(const Container<Int>::Iterator&, const Container<Int>::Iterator&);

This function is not a template, so there's pretty much no way to define operator== for all possible Containers outside the class template definition.

If you try to do

template<class T>
bool operator==(const Container<T>::Iterator&, const Container<T>::Iterator&);

That's a function template, and doesn't match the friend declaration. (In this case it's even worse, as you can't actually use this operator because T is in a non-deduced context.)

The warning message suggests one possible fix - first declaring a function template and then befriending a specialization of it. (You'll need to pull Iterator out of the class into its own separate class template so that T can be deduced.) The other possible fix is to just define the function inside the class template definition.

这篇关于如何正确地声明一个模板类的嵌套类的朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆