C++ C2977:“std::tuple”:模板参数太多 (MSVC11)

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

C2977: 'std::tuple' : too many template arguments (MSVC11)

c++visual-studio-2012c++11tuplesvariadic-templates

提问by Loom

I'm trying to build googletest with Visual C++ 11, but following code causes an error

我正在尝试使用 Visual C++ 11 构建 googletest,但以下代码导致错误

template <typename T1, typename T2, typename T3, typename T4, typename T5,
          typename T6, typename T7, typename T8, typename T9>
void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t, // <-- error C2977
             ::std::ostream* os) {
  PrintTupleTo(t, os);
}

That's an error text:

这是一个错误文本:

f:\gtest-1.6.0\include\gtest\gtest-printers.h(550): error C2977: 'std::tuple' : too many template arguments
  c:\program files (x86)\microsoft visual studio 11.0\vc\include\utility(72) : see declaration of 'std::tuple'

And there is the line 72 of utility-file:

还有utility-file的第 72 行:

template<class = _Nil, _MAX_CLASS_LIST>
   class tuple; // Line 72

What is the problem with std::tupleand how to fix it?

有什么问题std::tuple以及如何解决?

(BTW: I'm tried unsuccessfully to change std::tr1::tupleto std::tuple)

(顺便说一句:我尝试更改std::tr1::tuple为 ,但未成功std::tuple

回答by jmsu

Check out this entry in the msdn blog. VC++11 doesn't have support for variadic templates. They have something they call faux variadics. Scroll down and you will see a paragraph on Faux variadics that talks about tuples. On that paragraph they say the default maximum number of parameters is 5. You can increase it to 10:

msdn 博客中查看此条目。VC++11 不支持可变参数模板。他们有一种他们称之为假可变参数的东西。向下滚动,您将看到一段关于 Faux variadics 的讨论元组的段落。在该段落中,他们说默认的最大参数数是 5。您可以将其增加到 10:

You can define _VARIADIC_MAX project-wide between 5 and 10 inclusive (it defaults to 5). Increasing it will make the compiler consume more memory, and may require you to use the /Zm option to reserve more space for PCHes.

您可以在 5 到 10 之间定义 _VARIADIC_MAX 项目范围(默认为 5)。增加它会使编译器消耗更多内存,并且可能需要您使用 /Zm 选项为 PCHe 保留更多空间。

They say they have a fix incoming to make the default 10 again.

他们说他们有一个修复程序可以再次使默认值 10。

回答by Jichao

In Visual Studio 2012 (VC11) _VARIADIC_MAXis by default defined as 5 in header <xstddef>:

在 Visual Studio 2012 (VC11)_VARIADIC_MAX中,header 中默认定义为 5 <xstddef>

#if !defined(_VARIADIC_MAX)
 #define _VARIADIC_MAX  5

#elif _VARIADIC_MAX < 5 || 10 < _VARIADIC_MAX
 #error _VARIADIC_MAX must be between 5 and 10, inclusive
#endif /* !defined(_VARIADIC_MAX) */

if you have multiple VC11 projects include <tuple>in a solution, it is better to set the macro to all by

如果您<tuple>在一个解决方案中包含多个 VC11 项目,最好将宏设置为 all

1) Shift click to select all C++ projects in your solution

1) Shift 单击以选择解决方案中的所有 C++ 项目

2) Properties, C/C++, Preprocessor, All Configurations All Platforms, Preprocessor Definitions, <Edit>

2) 属性、C/C++、预处理器、所有配置、所有平台、预处理器定义、 <Edit>

3) add before <different options>a row

3)在<different options>一行前添加

_VARIADIC_MAX=10;

You can change 10 to any number in 6~10.

您可以将 10 更改为 6~10 中的任意数字。

回答by Dmitry Ledentsov

Setting GTEST_HAS_TR1_TUPLEto 0 in gtest.hhelped in my case

在 gtest.h 中设置GTEST_HAS_TR1_TUPLE为 0对我有帮助

Update: of course, the less intrusive way is to define a precompiler flag GTEST_HAS_TR1_TUPLE=0. Check the answers about _VARIADIC_MAX=10- solves another half of the issue.

更新:当然,侵入性较小的方法是定义一个预编译器标志 GTEST_HAS_TR1_TUPLE=0。检查关于_VARIADIC_MAX=10- 解决另一半问题的答案。

回答by Yakk - Adam Nevraumont

In Visual Studio 2013, std::tupleno longer uses _VARIADIC_MAXand now uses actual variardic templates, so this problem should be gone.

在 Visual Studio 2013 中,std::tuple不再使用_VARIADIC_MAX,现在使用实际的 variardic templates,所以这个问题应该消失了。

If you run into it in 2013, it means you are including the wrong standard library.

如果您在 2013 年遇到它,则意味着您包含了错误的标准库。

回答by Praveer Kumar

To use GoogleTest in Visual Studio 2012, you should set _VARIADIC_MAX=10 under Properties->C/C++->Preprocessor->PreprocessorDefinitions on the projects that use it. else you might encounter with below error error C2977: 'std::tuple' : too many template arguments

要在 Visual Studio 2012 中使用 GoogleTest,您应该在使用它的项目的 Properties->C/C++->Preprocessor->PreprocessorDefinitions 下设置 _VARIADIC_MAX=10。否则您可能会遇到以下错误错误 C2977: 'std::tuple' : too many template arguments

回答by Corey Kosak

回答by Validus Oculus

Add below line into your cmake file

将以下行添加到您的 cmake 文件中

add_definitions(/D_VARIADIC_MAX=10)