C ++ 11 std :: function限制函数指针可以拥有的参数数量? [英] Does C++11 std::function limit the number of arguments a function pointer can have?

查看:1415
本文介绍了C ++ 11 std :: function限制函数指针可以拥有的参数数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 11测试版,我很好奇编译错误,我在我的类中存储std :: function对象。

  typedef std :: function< void(int,const char *,int,int,const char *)> MyCallback; 

在我的班上,我有

  MyCallback m_callback; 

这个编译很好。如果我向列表中添加一个参数,它将失败。

  typedef std :: function< void(int,const char * int,int,const char *,int)> MyCallback; 

失败是:

 > c:\program files(x86)\microsoft visual studio 11.0 \vc\include\functional(535):错误C2027:使用未定义类型'std :: _ Get_function_impl& _Tx>'
1> with
1> [
1> _Tx = void(int,const char *,int,int,const char *,int)
1> ]
1> f:\development\projects\applications\my.h(72):参见类模板实例化'std :: function< _Fty>'被编译
1> with
1> [
1> _Fty = void(int,const char *,int,int,const char *,int)
1> ]
1> c:\program files(x86)\microsoft visual studio 11.0 \vc\include\functional(536):error C2504:'type':base class undefined
1> ; c:\program files(x86)\microsoft visual studio 11.0 \vc\include\functional(539):error C2027:使用未定义类型'std :: _ Get_function_impl< _Tx>'
1> ; with
1> [
1> _Tx = void(int,const char *,int,int,const char *,int)
1> ]
1> c:\program files(x86)\microsoft visual studio 11.0 \vc\include\functional(539):错误C2146:语法错误:在标识符'_Mybase'
1> c:\program files(x86)\microsoft visual studio 11.0 \vc\include\functional(539):error C4430:缺少类型说明符 - int。注意:C ++不支持default-int

这是一个动态链接库,到另一个应用程序。我可以修改数据的格式,使它可以通过更少的参数,但我想知道为什么我看到这个限制?



切换回c-风格函数指针,

  typedef void(* MyCallback)(int,const char *,int,int,const char *,int ); 

似乎工作得很好。



std :: function

的C ++规范

/ code>不设置任何限制。 std :: function 使用可变参数模板处理任意数量的参数。实现可以具有基于例如模板实例化嵌套限制的限制,但是它应该是大的。该规范建议1024作为一个良好的最小支持嵌套深度,256为一个很好的最小的一个函数调用允许的参数。



Visual Studio )没有可变参数模板。它们在VS11中模拟它们多达5个参数,但是您可以将其更改为高达10.通过在项目中定义 _VARIADIC_MAX 执行此操作。这可以大大增加编译时间。



更新:VS 2012年11月CTP增加了对可变参数模板的支持,但标准库尚未更新使用它们。一旦更新,你应该能够使用 std :: function 你想要的参数。


I'm using the Visual Studio 11 beta and I'm curious about a compilation error i'm getting storing a std::function object in my class.

typedef std::function<void (int, const char*, int, int, const char*)> MyCallback;

In my class I have,

MyCallback m_callback;

This compiles just fine. If I add one more argument to the list it fails.

typedef std::function<void (int, const char*, int, int, const char*, int)> MyCallback;

The failure is:

>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(535): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1>          with
1>          [
1>              _Tx=void (int,const char *,int,int,const char *,int)
1>          ]
1>          f:\development\projects\applications\my.h(72) : see reference to class template instantiation 'std::function<_Fty>' being compiled
1>          with
1>          [
1>              _Fty=void (int,const char *,int,int,const char *,int)
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(536): error C2504: 'type' : base class undefined
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(539): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>'
1>          with
1>          [
1>              _Tx=void (int,const char *,int,int,const char *,int)
1>          ]
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(539): error C2146: syntax error : missing ';' before identifier '_Mybase'
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(539): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

This is a dynamically linked library which is preparing data to pass to another application. I can certainly rework the format of the data so that it can be passed with fewer arguments, but I was wondering why I see this limit?

Switching back to the c-style function pointer,

 typedef void (*MyCallback)(int, const char*, int, int, const char*, int);

seems to work fine.

解决方案

This limit is set by the implementation in Visual Studio.

The C++ specification for std::function does not place any limit. std::function uses variadic templates to work with any number of arguments. Implementations may have a limit based on, e.g., template instantiation nesting limits, but it should be large. The spec suggests 1024 as a good minimum supported nesting depth, and 256 as a good minimum for arguments allowed in one function call, for example.

Visual Studio (as of VS11) does not have variadic templates. They simulate them up to 5 arguments in VS11, though you can change it to up to 10. Do this by defining _VARIADIC_MAX in your project. This can increase compile times greatly.

Update: The VS 2012 Nov CTP adds support for variadic templates, but the standard library has not yet been updated to use them. Once it is updated you should be able to use as many arguments as you want with std::function.

这篇关于C ++ 11 std :: function限制函数指针可以拥有的参数数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆