gpt4 book ai didi

c++ - 警告定义在命名空间内声明的友元运算符

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:28 26 4
gpt4 key购买 nike

有人可以向我解释来自 g++ 的警告吗?

给定以下代码

#include <iostream>

namespace foo
{
struct bar
{ friend std::ostream & operator<< (std::ostream &, bar const &); };
}

std::ostream & foo::operator<< (std::ostream & o, foo::bar const &)
{ return o; }

int main ()
{
foo::bar fb;

std::cout << fb;
}

我得到(来自 g++ (6.3.0) 但不是来自 clang++ (3.8.1) 而不是(感谢 Robert.M)来自 Visual Studio(2017 社区))这个警告

tmp_002-11,14,gcc,clang.cpp:10:16: warning: ‘std::ostream& foo::operator<<(std::ostream&, const foo::bar&)’ has not been declared within foo
std::ostream & foo::operator<< (std::ostream & o, foo::bar const &)
^~~
tmp_002-11,14,gcc,clang.cpp:7:29: note: only here as a friend
{ friend std::ostream & operator<< (std::ostream &, bar const &); };
^~~~~~~~

我知道我可以如下定义运算符

namespace foo
{
std::ostream & operator<< (std::ostream & o, bar const &)
{ return o; }
}

但是...我的初始代码有什么问题?

最佳答案

n.m.的答案是的,虽然我需要更多的挖掘才能明白为什么,所以这里有一些我关注的链接:

CppCoreGuidelines 解释 "Nonmember operators should be either friends or defined in the same namespace as their operands" .这里有一个更详细的解释 why the same namespace .
也许这个this message from the GCC mailing list提供了更多见解:看起来 GCC 人员似乎决定在 2016 年的某个时候更严格地处理这个问题。

这里的关键是命名空间。如果您的代码在命名空间 foo 内而不是在外部定义了 operator<<,那么您的代码就没问题,如下所示:

namespace foo
{
struct bar
{
friend std::ostream & operator<< (std::ostream &, bar const &);
};

// Implementation
std::ostream & operator<< (std::ostream & o, foo::bar const &)
{ return o; }
}

请注意,如果将实现直接与 friend 定义放在一起,这会变得更简单,如 this SO answer to a similar question 所示。 .

这是对 very similar question 的另一个非常详尽的 SO 答案.当我试图解决同样的问题时,它帮助了我(gcc 7 向我发出警告,警告我可以用更旧的版本编译得很好的代码)。

关于c++ - 警告定义在命名空间内声明的友元运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44749878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com