首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加<iostream>会破坏g++-7中的代码

添加<iostream>会破坏g++-7中的代码
EN

Stack Overflow用户
提问于 2017-09-11 19:27:38
回答 1查看 323关注 0票数 6

有样品

代码语言:javascript
复制
// Example 2: Will this compile? 
//
// In some library header:
namespace N { class C {}; }
int operator+(int i, N::C) { return i+1; }
// A mainline to exercise it:
#include <numeric>
int main()
{
  N::C a[10];
  std::accumulate(a, a+10, 0);
}

来自“例外C++:47个工程难题,编程问题和解决方案”名称查找和接口原理-第4部分

g++ 5.4成功地编译了它。但是添加#include <iostream>会破坏代码

代码语言:javascript
复制
// Example 2: Will this compile? 
//
// In some library header:
namespace N { class C {}; }
int operator+(int i, N::C) { return i+1; }
// A mainline to exercise it:
#include <numeric>
#include <iostream>
int main()
{
    N::C a[10];
    std::accumulate(a, a+10, 0);
}

clang-4.0能够编译它.g++ 5.4和g++7.2.0显示他跟随错误

代码语言:javascript
复制
In file included from /usr/include/c++/7/numeric:62:0,
                 from src/widget.cpp:7:
/usr/include/c++/7/bits/stl_numeric.h: In instantiation of ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp) [with _InputIterator = N::C*; _Tp = int]’:
src/widget.cpp:12:35:   required from here
/usr/include/c++/7/bits/stl_numeric.h:127:18: error: no match for ‘operator+’ (operand types are ‘int’ and ‘N::C’)
  __init = __init + *__first;
           ~~~~~~~^~~~~~~~~~

看上去像g++中的bug。我有兴趣知道是否存在解决办法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-11 20:02:23

如果有人像我这样好奇的话,我会把我从进一步阅读中了解到的东西张贴在书中。

编译器将查找从std::operator+调用的从命名空间std开始的累积。

只有在名称空间中没有找到候选人时,它才会向上查找全局名称空间候选项。

因此,clang中的原始样本和修改样本都是纯粹靠运气编写的,在std: operator+之前没有声明。

一旦新的标题被添加到游戏中,编译器就停止查看全局命名空间,并完全不再将适当的运算符视为候选。

最好的匹配不是那么好,并导致了所有这些奇怪的错误信息。

现在,将operator+迁移到命名空间N正在启动Koenig查找--如果函数参数之一在名称空间N中,则应该在此命名空间中查找合适的候选项,并在常规查找的基础上进行查找。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46163095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档