C++ 一个如何包含TR1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228402/
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-27 19:14:03  来源:igfitidea点击:

How does one include TR1?

c++includec++11portabilitytr1

提问by Grumbel

Different compilers seem to have different ideas about TR1. G++ only seems to accept includes of the type:

不同的编译器似乎对 TR1 有不同的想法。G++ 似乎只接受以下类型的包含:

#include <tr1/unordered_map>
#include <tr1/memory>
...

While Microsofts compiler only accept:

而微软的编译器只接受:

#include <unordered_map>
#include <memory>
...

As for as I understand TR1, the Microsoft way is the correct one.

至于我对TR1的理解,微软的方式是正确的。

Is there a way to get G++ to accept the second version? How does one in general handle TR1 in a portable way?

有没有办法让 G++ 接受第二个版本?一般人如何以便携的方式处理 TR1?

采纳答案by Martin York

Install boost on your machine.
Add the following directory to your search path.

在您的机器上安装 boost。
将以下目录添加到您的搜索路径。

<Boost Install Directory>/boost/tr1/tr1

<Boost 安装目录>/boost/tr1/tr1

see here boost tr1for details

有关详细信息,请参见此处的boost tr1

Now when you include <memory> you get the tr1 version of memory that has std::tr1::shared_ptr and then it includes the platform specific version of <memory> to get all the normal goodies.

现在,当您包含 <memory> 时,您将获得具有 std::tr1::shared_ptr 的 tr1 版本的内存,然后它包含特定于平台的 <memory> 版本以获得所有正常的好东西。

回答by mtd

#ifdef _WIN32
    #include <unordered_map>
    #include <memory>
#else
    #include <tr1/unordered_map>
    #include <trl/memory>
#endif

回答by bdonlan

Perhaps the best way would be to simply use boostlibraries for now, as in many cases they have alternatives with a similar interface to TR1 features, and are just in a different (but consistent) header path and namespace. This has the advantage of working on compilers that haven't even begunimplementing C++0x. And there are plenty of useful boost libraries that aren't in TR1 at all :)

也许现在最好的方法是简单地使用boost库,因为在许多情况下,它们具有与 TR1 功能类似的接口的替代方案,并且只是在不同(但一致)的标头路径和命名空间中。这具有在甚至还没有开始实现 C++0x 的编译器上工作的优势。并且有很多有用的 boost 库根本不在 TR1 中:)

Alternately, on G++, you could try passing --std=gnu++0x on the command line. This works for <unordered_set> and <unordered_map>, at least. Then to make it available in std::tr1:

或者,在 G++ 上,您可以尝试在命令行上传递 --std=gnu++0x 。这至少适用于 <unordered_set> 和 <unordered_map>。然后使其在 std::tr1 中可用:

namespace std { namespace tr1 { using namespace std; } }

This is evil, naturally. I highly recommend the boost approach instead :)

这自然是邪恶的。我强烈推荐使用 boost 方法:)

回答by jon-hanson

A tad hacky perhaps, but you could simply add the compiler tr1 directory to your include path.

也许有点 hacky,但您可以简单地将编译器 tr1 目录添加到您的包含路径中。

回答by John Dibling

If under Windows, add the 'tr1' directory to the system path. Then #include <memory>should work.

如果在 Windows 下,请将 'tr1' 目录添加到系统路径中。然后#include <memory>应该工作。

回答by sellibitze

I asked myself the same question. Unfortunately, the technical report doesn't say how the headers should be included. It only defines that the extensions should be in the ::std::tr1 namespace.

我问自己同样的问题。不幸的是,技术报告没有说明应该如何包含标题。它只定义扩展应该在 ::std::tr1 命名空间中。