std::tr1 与 visual studio 2017

std::tr1 with visual studio 2017

我有一些 C++ 代码使用了 Google 的 GTest 框架的某个版本。这段代码过去用 Visual Studio 2015 编译得很好。我刚升级到 VS2017,现在我得到了一堆这样的错误:

error C2039: 'tr1': is not a member of 'std'
error C3083: 'tr1': the symbol to the left of a '::' must be a type

在 VS2017 中使用 std::tr1 需要一些编译器选项吗?

一个选项是re-enable TR1;通过定义宏 _HAS_TR1_NAMESPACE 来做到这一点,如 this blog article. If you're using an MSBuild project then this is best done by way of your project's Preprocessor Definitions 设置中简要提到的那样;或者,如果您使用的是预编译的 header,请在上述 PCH 的顶部定义它。

一个更好的选择是通过在包含任何 GTest header 之前定义宏 GTEST_LANG_CXX111 来通知 GTest 您的编译器支持 C++11;那么它将使用 std::tuple 而不是 std::tr1::tuple*。 (GTest's C++11-detection logic 是面向 __cplusplus 的,VC++ 尚未更新,尽管它主要与 C++11 和 C++14 兼容。我会说这是 GTest 中的一个错误因为它在整个配置逻辑的其他地方支持 VC++。)

* 更不用说其他 C++11 功能,这就是为什么这是迄今为止更好的选择;-]

Googletest release 1.8.1 修复了这个问题(结合 VS2017 15.8.5)。