C++ std、tr1 和 boost(作为命名空间和/或库)之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4682919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:08:57  来源:igfitidea点击:

What are differences between std, tr1 and boost (as namespaces and/or libraries)?

c++c++11boosttr1

提问by Chan

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:

我一开始以为它们都是一样的,但结果证明是错误的。那么谁能简单解释一下这三者之间的区别?例如:

  1. std::bind( newest one, next generation of C++ )
  2. std::tr1::bind( old, extension of C++ std )
  3. boost::bind( completely separate library )
  1. std::bind( 最新的,下一代 C++ )
  2. std::tr1::bind(旧的,C++ std 的扩展)
  3. boost::bind(完全独立的库)

or std::shared_ptr, std::tr1::shared_ptr, and boost::shared_ptr, ...etc

std::shared_ptr, std::tr1::shared_ptr, 和boost::shared_ptr, ...等

Update

更新

bind, shared_ptrare 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 bindis one example, as well as shared_ptr.

bindshared_ptr是有助于澄清我的问题的例子。我的目的是了解这三个命名空间之间的一般差异。在所有三个命名空间中都存在多个库,显然bind是一个示例,还有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 ).

我应该坚持使用哪些命名空间?我个人更喜欢库 fromstd::因为它将成为 C++ ( C++0x ) 的下一个标准。

采纳答案by wkl

1 - std::bindis 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++.

1 -std::bind是它的标准名称。这将是您用于 C++11 兼容库的名称。标准化 C++中所有库的列表。

2 - std::tr1::bindis 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 contains std::bind). The std::tr1namespace was used to differentiate the libraries in their work-in-progress state, as opposed to everything standardized in the stdnamespace.

2 -std::tr1::bind是 C++ 技术报告 1 命名空间。在 C++03 和 C++11 之间有C++ Technical Report 1,它提出了额外的库和增强功能。其中大部分在当时已经存在于 Boost 中,其中一些库更改被 C++11 标准采用,例如<regex><functional>(其中包含std::bind)。该std::tr1命名空间是用来区分他们的工作中状态的库,而不是一切都在标准化的std命名空间。

3 - boost::bindis for bindin the boostnamespace, if you are using the Boostlibrary. 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.0

3 -boost::bindbindboost命名空间中,如果您使用的是加速库。Boost 包含的内容远不止 TR1 中的内容和 C++11 标准库中的内容。自 1.52.0 起 Boost中所有库的列表

Most of what was in TR1 has been standardized and is in the C++11 stdnamespace, and C++11 contains more libraries than mentioned in TR1 that were adapted from Boost constructs, like threading support defined in <thread>.

TR1 中的大部分内容都已标准化并位于 C++11std命名空间中,并且 C++11 包含比 TR1 中提到的更多的库,它们改编自 Boost 构造,例如<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 stdnamespaces for the new C++11 libraries, but might require a different compiler flag to activate that. They will still support the std::tr1namespace though. Visual C++ 2010 moved what was previously in std::tr1into the normal stdnamespace, but Visual C++ 2008 still used std::tr1.

部分定义您可以使用的内容以及您现在可以使用的命名空间取决于您的编译器。我不记得了,但我认为最近的 GCC-g++ 实现已经开始std为新的 C++11 库使用命名空间,但可能需要不同的编译器标志来激活它。他们仍然会支持std::tr1命名空间。Visual C++ 2010 将以前的内容移动std::tr1到普通std命名空间中,但 Visual C++ 2008 仍然使用std::tr1.

回答by MatiasFG

If you want to use bind (or any other for the matter), a nice feature is namespace renaming, here is an example:

如果你想使用绑定(或任何其他的),一个很好的特性是命名空间重命名,这是一个例子:

namespace MyNamespace = boost;

void DoSomething(void)
{
    MyNamespace::bind( ... );
}

Now, if you change MyNamespace to be:

现在,如果您将 MyNamespace 更改为:

namespace MyNamespace = std::tr1;

The following uses std::tr1::bind.

以下使用std::tr1::bind.

namespace MyNamespace = std::tr1;

void DoSomething(void)
{
    MyNamespace::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.

当然,您应该将 MyNamespace 用于将来想要轻松更改其命名空间的元素,如果您知道需要 std::tr1 ,则应该直接使用它,而不要使用别名。

回答by Edward Strange

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.

1) 究竟如何以及为什么 std:: 被 tr1 扩展。TR1 是“技术报告 1”,是标准委员会的一个小组向标准委员会提出的第一个正式的库扩展集。所以它不仅仅是标准的扩展。

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 with std::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.

2) boost::bind 实际上与 std::bind 的行为不同,至少在某些系统上是这样。我不知道它是否符合标准,但在 MSVC lambda 表达式中,std::bind 彼此之间的表现非常差。也许还有其他一些方式,我不记得了,因为我制定了使用 boost::bind 而不是 std::bind 的政策。msvc 上的 std::bind 似乎经常忽略返回值模板参数,因此return_value<f>::type当您使用std::bind<type>(...). 从来没有费心去弄清楚行为的确切差异,因为 boost::bind 已经将它纳入我们的常规词汇表并且我们知道如何使用它。

回答by Philipp

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::bindand don't have to be compatible with other compilers, just use it.boost::bindis good if you want to be compiler-independent. I think std::tr1::binddoesn't have any advantages over the other two if they are available: it is nonstandard with respect to both C++03 and C++0x.

它不应该有太大的不同,因为下一个 C++ 标准的大部分实际上是从 Boost 继承的。因此,如果您已经std::bind并且不必与其他编译器兼容,请使用它。boost::bind如果您想独立于编译器,这很好。std::tr1::bind如果它们可用,我认为与其他两个相比没有任何优势:就 C++03 和 C++0x 而言,它都是非标准的。