友元声明声明一个非模板函数

发布于 2024-09-29 02:26:45 字数 1319 浏览 3 评论 0原文

我有一个类似于下面代码的基类。我正在尝试超载<<与 cout 一起使用。 然而,g++ 说:

base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function
base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning

我尝试添加 <>之后<<在类声明/原型中。但是,然后我发现与任何模板声明都不匹配。我一直在尝试将运算符定义完全模板化(这是我想要的),但我只能让它与以下代码一起使用,并手动实例化运算符。

base.h

template <typename T>
class Base {
  public:
    friend ostream& operator << (ostream &out, Base<T> *e);
};

base.cpp

ostream& operator<< (ostream &out, Base<int> *e) {
    out << e->data;
return out;
}

我想在标题中包含这个或类似的内容,base.h:

template <typename T>
class Base {
  public:
    friend ostream& operator << (ostream &out, Base<T> *e);
};

template <typename T>
ostream& operator<< (ostream &out, Base<T> *e) {
    out << e->data;
return out;
}

我在网上其他地方读过,将 <>之间 <<原型中的 and () 应该可以解决这个问题,但事实并非如此。我可以将其放入单个函数模板中吗?

I have a base Class akin to the code below. I'm attempting to overload << to use with cout.
However, g++ is saying:

base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function
base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning

I've tried adding <> after << in the class declaration / prototype. However, then I get it does not match any template declaration. I've been attempting to have the operator definition fully templated (which I want), but I've only been able to get it to work with the following code, with the operator manually instantiated.

base.h

template <typename T>
class Base {
  public:
    friend ostream& operator << (ostream &out, Base<T> *e);
};

base.cpp

ostream& operator<< (ostream &out, Base<int> *e) {
    out << e->data;
return out;
}

I want to just have this or similar in the header, base.h:

template <typename T>
class Base {
  public:
    friend ostream& operator << (ostream &out, Base<T> *e);
};

template <typename T>
ostream& operator<< (ostream &out, Base<T> *e) {
    out << e->data;
return out;
}

I've read elsewhere online that putting <> between << and () in the prototype should fix this, but it doesn't. Can I get this into a single function template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美羊羊 2024-10-06 02:26:46

您可能正在寻找的是:

template <typename T>
class Base;

template <typename T>
ostream& operator<< (ostream &, const Base<T>&);

template <typename T>
class Base
{
  public:
    template<>
    friend ostream& operator << <T>(ostream &, const Base<T> &);
};

template <typename T>
ostream& operator<< ( ostream &out, const Base<T>& e )
{
    return out << e->data;
}

这只是模板的一个实例,其中运算符的模板参数与类的模板参数相匹配。

更新:不幸的是,这是非法的。 MSVC 和 Comeau 都拒绝了它。这就提出了一个问题:为什么原始错误消息几乎完全建议采用这种方法。

Probably what you are looking for is:

template <typename T>
class Base;

template <typename T>
ostream& operator<< (ostream &, const Base<T>&);

template <typename T>
class Base
{
  public:
    template<>
    friend ostream& operator << <T>(ostream &, const Base<T> &);
};

template <typename T>
ostream& operator<< ( ostream &out, const Base<T>& e )
{
    return out << e->data;
}

This friends only a single instantiation of the template, the one where the operator's template parameter matches the class's template parameter.

UPDATE: Unfortunately, it's illegal. Both MSVC and Comeau reject it. Which raises the question of why the original error message suggested pretty much EXACTLY this approach.

绝不服输 2024-10-06 02:26:46

更改

friend ostream& operator << (ostream& out, const Base<T>& e);

friend ostream& operator << <T>(ostream& out, const Base<T>& e);

应该也可以——我刚刚以这种方式解决了同样的问题。

changing

friend ostream& operator << (ostream& out, const Base<T>& e);

to

friend ostream& operator << <T>(ostream& out, const Base<T>& e);

should work as well--I just solved an identical problem in this way.

爱的十字路口 2024-10-06 02:26:46

更改

friend ostream& operator << (ostream &out, Base<T> *e)`

template<T> friend ostream& operator << (ostream &out, Base *e)

changing

friend ostream& operator << (ostream &out, Base<T> *e)`

To

template<T> friend ostream& operator << (ostream &out, Base *e)
故事和酒 2024-10-06 02:26:45

听起来您想更改:

friend ostream& operator << (ostream& out, const Base<T>& e);

至:

template<class T>
friend ostream& operator << (ostream& out, const Base<T>& e);

It sounds like you want to change:

friend ostream& operator << (ostream& out, const Base<T>& e);

To:

template<class T>
friend ostream& operator << (ostream& out, const Base<T>& e);
冬天旳寂寞 2024-10-06 02:26:45

Gcc 警告您是正确的。尽管它的外观(它采用 Base 参数),但它不是函数模板。

您的类定义具有友元函数的非模板声明(没有模板),但稍后的友元函数定义是函数模板(即以模板开头..)。

还有您的运营商<<需要一个 Base *。这是不正确的。它应该是 Base const &保留其内置语义

您可能正在查看如下内容:

template <typename T> 
class Base { 
  public: 
    friend ostream& operator << (ostream &out, Base<T> const &e){
       return out;
    }; 
}; 

int main(){
   Base<int> b;
   cout << b;
}

如果您想要完全模板化,那么这可能就是您想要的。但我不确定这比前一个有用多少。由于查找涉及 ADL,因此您将永远无法解析 T 不等于 U 的任何条件(只要调用来自与此类不相关的上下文,例如来自“main”函数)

template <typename T>  
class Base {  
  public:  
    template<class U> friend ostream& operator << (ostream &out, Base<U> const &e){ 
       return out; 
    };  
};

int main(){ 
   Base<int> b; 
   cout << b; 
} 

Gcc is rightly warning you. Despite it's appearances (it takes Base argument), it is not a function template.

Your class definition has a non-template declaration of the friend function (without the template), but the friend function definition later on is a function template (i.e. starts with template..).

Also your operator<< takes a Base *. This is not correct. It should be Base const & to retain it's built-in semantics

Probably you are looking at something as below:

template <typename T> 
class Base { 
  public: 
    friend ostream& operator << (ostream &out, Base<T> const &e){
       return out;
    }; 
}; 

int main(){
   Base<int> b;
   cout << b;
}

If you want fully templated, then this is probably what you want. But I am not sure how much useful this is over the previous one. Since the lookup involves ADL, you will never be able to resolve to any condition where T is not equal to U (as long as the call is from a context not related to this class e.g. from 'main' function)

template <typename T>  
class Base {  
  public:  
    template<class U> friend ostream& operator << (ostream &out, Base<U> const &e){ 
       return out; 
    };  
};

int main(){ 
   Base<int> b; 
   cout << b; 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文