【发布时间】:2015-03-24 13:44:09
【问题描述】:
我正在使用 C++14(-std=c++1y 在 g++ 4.9.1 和 clang 3.5 上)。
首先,这里是 Exhibit A(存在 Foo 命名空间):
#include <iostream>
#include <sstream>
namespace Foo
{
struct A
{};
}
void operator<<(std::ostream &os, Foo::A const &a)
{}
int main()
{
Foo::A a;
std::ostringstream() << a;
return 0;
}
Clang 和 g++ 都对此表示反对,尽管出于不同的原因。
Exhibit B(没有 Foo 命名空间):
#include <iostream>
#include <sstream>
struct A
{};
void operator<<(std::ostream &os, A const &a)
{}
int main()
{
A a;
std::ostringstream() << a;
return 0;
}
g++ 仍然出错,但 Clang 成功编译。
这是合理的预期吗?这是怎么回事?
【问题讨论】:
-
我见过一些关于将临时值绑定到非常量引用的 cmets。我要提出两点:首先,通过将上面的代码更改为 void operator
-
@NeilKirk 我认为您不小心编译了(工作)左值版本。这是我遇到麻烦的右值。
-
当operator
std::operator<<,它接受basic_ostream<CharT,Traits>&&。 -
@NeilKirk 我发现这是 Clang 和 g++ 之间的区别。当结构 A 不在命名空间中时,Clang 将编译代码而 g++ 不会。当 struct A 在 Foo 命名空间中时,编译器都不喜欢它。
-
这是一个库问题。这是 libstdc++ vs libc++。
标签: c++ namespaces operator-overloading rvalue