Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++11 std::function limit the number of arguments a function pointer can have?

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.

like image 390
BZor Avatar asked Apr 10 '12 15:04

BZor


People also ask

Is std :: function a function pointer?

No. One is a function pointer; the other is an object that serves as a wrapper around a function pointer. They pretty much represent the same thing, but std::function is far more powerful, allowing you to do make bindings and whatnot.

What is the maximum number of parameters which can be used in a function definition?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

What is the maximum number of parameters accepted by open?

— Parameters in one macro definition [256].

How many arguments we can pass to a class member function if we are not specifying the total number of arguments during declaration?

It can be unary (one parameter) or binary (two parameters). When you overload this operator by a member function one parameter is already implied, so you can only add zero or one additional parameters (for unary * and binary * respectively).


1 Answers

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.

like image 169
bames53 Avatar answered Sep 28 '22 22:09

bames53