std、tr1 和 boost(作为命名空间和/或库)之间有什么区别?
我最初以为它们都是一样的,但事实证明是错误的。那么谁能简单解释一下这三者之间的区别?例如:
std::bind
(最新的,下一代 C++)std::tr1::bind
(旧的,C++ std 的扩展)boost: :bind
(完全独立的库)
或 std::shared_ptr
、std::tr1::shared_ptr
和 boost::shared_ptr
>, ...etc
Update
bind
, shared_ptr
是有助于澄清我的问题的示例。我的目的是了解这三个名称空间之间的一般差异。所有三个命名空间中都存在多个库,显然 bind
就是一个例子,还有 shared_ptr
。
我应该坚持使用哪些命名空间?我个人更喜欢 std::
中的库,因为它将成为 C++ ( C++0x ) 的下一个标准。
I initially thought they're all the same, but it turned out to be wrong. So can anyone briefly explain the differences between these three? For example:
std::bind
( newest one, next generation of C++ )std::tr1::bind
( old, extension of C++ std )boost::bind
( completely separate library )
or std::shared_ptr
, std::tr1::shared_ptr
, and boost::shared_ptr
, ...etc
Update
bind
, shared_ptr
are examples that help to clarify my question. My intention was to understand the general differences between those three namespaces. There are several libraries that exist in all three namespaces, and apparently bind
is one example, as well as shared_ptr
.
What namespaces should I stick with? I personally prefer library from std::
since it will be the next standard of C++ ( C++0x ).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1 -
std::bind
是它的标准名称。这将是您用于 C++11 兼容库的名称。所有标准化 C++ 库的列表。2 -
std::tr1::bind
是 C++ 技术报告 1 命名空间。在 C++03 和 C++11 之间有 C++ 技术报告 1 ,其中提出了额外的库和增强功能。其中大部分当时已经存在于 Boost 中,其中一些库更改已在 C++11 标准中采用,例如
和
> (其中包含std::bind
)。std::tr1
命名空间用于区分处于工作状态的库,而不是在std
命名空间中标准化的所有内容。3 -
boost::bind
用于boost
命名空间中的bind
(如果您使用的是 提升库。 Boost 包含的内容远不止 TR1 中的内容和 C++11 的 std 库中的内容。 Boost 中的库的列表自 1.52.0起 TR1 中的内容已经标准化,并且位于 C++11
std
命名空间中,并且 C++11 包含的库比 TR1 中提到的更多,这些库改编自 Boost 构造,例如 < a href="http://en.cppreference.com/w/cpp/thread" rel="noreferrer">
。定义您现在可以使用什么以及您可以使用哪个命名空间的部分内容取决于您的编译器。我不记得了,但我认为最近的 GCC-g++ 实现已经开始为新的 C++11 库使用 std 命名空间,但可能需要不同的编译器标志来激活它。不过,它们仍然支持
std::tr1
命名空间。 Visual C++ 2010 将以前位于std::tr1
中的内容移至普通std
命名空间,但 Visual C++ 2008 仍使用std::tr1
。1 -
std::bind
is the the standard name for it. This will be the name you use for C++11 compliant libraries. List of all libraries in standardized C++.2 -
std::tr1::bind
is C++ Technical Report 1 namespace. Between C++03 and C++11 there was the C++ Technical Report 1, which proposed additional libraries and enhancements. Most of these already existed in Boost at the time, and some of these library changes were adopted in the C++11 standard, like<regex>
and<functional>
(which containsstd::bind
). Thestd::tr1
namespace was used to differentiate the libraries in their work-in-progress state, as opposed to everything standardized in thestd
namespace.3 -
boost::bind
is forbind
in theboost
namespace, if you are using the Boost library. Boost encompasses much more than what is in TR1 and what i in C++11's std library. List of all libraries in Boost as of 1.52.0Most of what was in TR1 has been standardized and is in the C++11
std
namespace, and C++11 contains more libraries than mentioned in TR1 that were adapted from Boost constructs, like threading support defined in<thread>
.Part of what defines what you can use and which namespace you can use now depends on your compiler. I don't recall, but I think the more recent GCC-g++ implementations have started using
std
namespaces for the new C++11 libraries, but might require a different compiler flag to activate that. They will still support thestd::tr1
namespace though. Visual C++ 2010 moved what was previously instd::tr1
into the normalstd
namespace, but Visual C++ 2008 still usedstd::tr1
.如果您想使用bind(或任何其他相关的),一个很好的功能是命名空间重命名,这里有一个示例:
现在,如果您将MyNamespace更改为:
以下使用
std::tr1::bind< /代码>。
当然,您应该对将来想要轻松更改其名称空间的元素使用 MyNamespace,如果您知道需要 std::tr1,则应该直接使用它而不是别名。
If you want to use bind (or any other for the matter), a nice feature is namespace renaming, here is an example:
Now, if you change MyNamespace to be:
The following uses
std::tr1::bind
.You should, of course, use MyNamespace for elements that you want to easily change it's namespace in the future, if you know you want std::tr1 you should use it directly and never an alias.
你的问题已经差不多了。我可以复制/粘贴您的示例并正确回答您的问题。只有两件事确实需要扩展:
1)究竟如何以及为什么 std:: 被 tr1 扩展。 TR1 是“技术报告 1”,是标准委员会的一个小组向标准委员会提出的第一套正式的库扩展。所以它不仅仅是标准的扩展。
2) boost::bind 实际上的行为与 std::bind 不同,至少在某些系统上是这样。我不知道这是否符合标准,但在 MSVC lambda 表达式和 std::bind 中彼此表现得很差。也许还有其他一些方法,我不记得了,因为我制定了使用 boost::bind 而不是 std::bind 的政策。返回值模板参数似乎经常被 msvc 上的 std::bind 忽略,因此当您指定它时,您会收到有关没有
return_value::type
(或其他)的错误与std::bind(...)
。自从 boost::bind 已经进入我们的常规词汇并且我们知道如何使用它以来,我们从来没有费心去弄清楚行为上的确切差异。You've pretty much got it in your question there. I could just copy/paste your example and answer your question correctly. Only two thing really stand out as needing expansion:
1) Exactly HOW and why std:: is expanded by tr1. TR1 is "Technical Report 1" and is the first official set of library expansions proposed to the standards committee by one of its subgroups. So it's a little more than just an extension of the standard.
2) boost::bind actually behaves differently than std::bind, at least on some systems. I don't know if it's by standard on not but in MSVC lambda expressions and std::bind behave very poorly with each other. Maybe some other ways too, I don't recall since I made it policy to use boost::bind rather than std::bind. The return value template parameter seems to often be ignored with std::bind on msvc so that you get errors about there being no
return_value<f>::type
(or whatever) when you've specified it withstd::bind<type>(...)
. Never bothered to figure out the exact difference in behavior since boost::bind had already made it into our regular vocabulary and we knew how to use it.它应该不会产生太大的差异,因为下一个 C++ 标准的大部分实际上是从 Boost 继承的。因此,如果您有
std::bind
并且不必与其他编译器兼容,只需使用它。如果您想成为编译器,boost::bind
很好-独立的。我认为 std::tr1::bind 相对于其他两个(如果可用的话)没有任何优势:它对于 C++03 和 C++0x 来说都是非标准的。It shouldn't make a big difference since large parts of the next C++ standard were in fact inherited from Boost. So if you have
std::bind
and don't have to be compatible with other compilers, just use it.boost::bind
is good if you want to be compiler-independent. I thinkstd::tr1::bind
doesn't have any advantages over the other two if they are available: it is nonstandard with respect to both C++03 and C++0x.