为什么boost :: bind坚持将`boost :: placeholders`放到全局名称空间中? [英] Why boost::bind insists pulling `boost::placeholders` into global namespace?

查看:43
本文介绍了为什么boost :: bind坚持将`boost :: placeholders`放到全局名称空间中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可以轻松修复,但很烦人.

The following code can be fixed easily, but quite annoying.

#include <functional>
#include <boost/bind.hpp>
void foo() {
  using namespace std::placeholders;
  std::bind(_1, _2, _3); // ambiguous
}

有一个宏 BOOST_BIND_NO_PLACEHOLDERS ,但是使用此宏也会带来一些弊端,例如导致 boost :: placeholders 从包含的编译单元中消失< boost/bind.hpp> ,但不包含< boost/bind/placeholders.hpp> .

There's a macro BOOST_BIND_NO_PLACEHOLDERS, but using this macro will also bring some drawbacks like causing boost::placeholders disappear from the compile unit included <boost/bind.hpp> but not included <boost/bind/placeholders.hpp>.

名称冲突也与其他库一起出现,例如 boost :: mpl ,我不认为维护人员不知道问题所在,但我想知道为什么他们坚持不弃用和在< boost/bind.hpp> 中使用命名空间boost :: placeholders 删除.

The name conflicts also comes with other libs like boost::mpl, I don't think the maintainers don't know the problem, but I want to know why they insist on not deprecating and deleting using namespace boost::placeholders in <boost/bind.hpp>.

推荐答案

新版本的boost中似乎已解决了该问题.

Looks like it has been fixed in newer versions of boost.

当包含 boost/bind.hpp 时,我们会收到以下消息:

When including boost/bind.hpp we get this message:

#pragma消息:不建议在全局名称空间中声明绑定占位符(_1,_2,...)的做法.请使用< boost/bind/bind.hpp>+使用命名空间boost :: placeholders,或定义BOOST_BIND_GLOBAL_PLACEHOLDERS来保留当前行为.

该解决方案在 https://www.boost中进行了描述.org/doc/libs/1_73_0/boost/bind.hpp

因此,好的做法"解决方法是代替

So the "good practice" fix is to instead of

#include< boost/bind.hpp> 这将boost :: placeholders放在了全局命名空间中

#include <boost/bind.hpp> which puts the boost::placeholders in global namespace

#include< boost/bind/bind.hpp> 这不会将boost ::占位符放在全局名称空间中.然后直接使用限定名称,例如 boost :: placeholders :: _ 1 ,或在本地使用名称空间boost :: placeholders

#include <boost/bind/bind.hpp> which does not put the boost:: placeholders in global namespace. Then use the qualified names like boost::placeholders::_1 directly, or locally do using namespace boost::placeholders

这篇关于为什么boost :: bind坚持将`boost :: placeholders`放到全局名称空间中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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