Sign in to follow this  
REspawn

[C++] Logging function arguments

Recommended Posts

Hi,

Does anyone know of a macro or easy way to loop through a functions arguments in C++? I'm not talking about variable arguments, just the normal fixed ones.

Thanks for any help,

Dave







Share this post


Link to post
Share on other sites
[quote name='REspawn' timestamp='1302630127' post='4797581']
Does anyone know of a macro or easy way to loop through a functions arguments in C++? I'm not talking about variable arguments, just the normal fixed ones.
[/quote]
What are variable and fixed arguments?

Depending on what you want to do exactly, you might be able to use one of these:

[list]
[*][url="http://www.boost.org/doc/libs/1_46_1/libs/function_types/doc/html/index.html"]boost::function_types[/url]
[*]The [url="http://www.boost.org/doc/libs/1_46_1/libs/preprocessor/doc/index.html"]boost preprocessor library[/url] to generate functions with different arity overloads
[*]C++11 variadic templates
[*]Some 'clever' variadic macro usage
[/list]

It is probably worth mentioning explicitly what you're trying to achieve in broader terms as someone may have a better solution for your problem than iterating over arguments.

Share this post


Link to post
Share on other sites
[quote name='REspawn' timestamp='1303942651' post='4803738']
Thanks for the reply guys. My end goal would be in the debug build to log all arguments passed into a function. For profiling purposes.
[/quote]

Why wouldn't normal logging of the parameters work here? Even with extreme templatization, most logging procedures should do the trick.

Share this post


Link to post
Share on other sites
[quote name='Steno' timestamp='1303965442' post='4803841']
[quote name='REspawn' timestamp='1303942651' post='4803738']
Thanks for the reply guys. My end goal would be in the debug build to log all arguments passed into a function. For profiling purposes.
[/quote]

Why wouldn't normal logging of the parameters work here? Even with extreme templatization, most logging procedures should do the trick.
[/quote]

It would. I was just researching to see if there was a nice macro / template way to do this. So all you had to do was stick macro at the top of each function and it would work regardless of the number of arguments.

Share this post


Link to post
Share on other sites
pre-processing the source with your own script
or using some other metaobject compiler might be the 'cleanest' choice

variadic macros probably won't be pretty:
[url="http://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros"]http://stackoverflow...variadic-macros[/url]
[url="http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/"]http://cplusplus.co....r-of-arguments/[/url]
[url="https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/"]https://gustedt.word...acro-arguments/[/url]

Share this post


Link to post
Share on other sites
You can do it with symbol information, same way that debuggers can print out the information in the call stack window. If you're using Windows and Visual Studio, check out the DbgHelp functions. I wouldn't recommend it for profiling purposes, since accessing symbol information is slow, but I wouldn't recommend logging at all during profiling.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this