Sign in to follow this  

[C++] - Call func for each variadic term?

This topic is 2586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I asked this before on the general forum (no response) but perhaps it should be here? (Tidied up question too).

I want to call a template function for each parameter passed to another function's variadic parameter list.
The template function (eg: inc() below) takes one parameter of any type, and returns void.

Eg, if I call qwert(a, b, c) I want qwert() to call inc(a), inc(b), inc(c), where:
- a, b and c can be of any type.
- inc() returns nothing.

I'm looking for the cleanest way to do this. Something like [#1] (comment in source) is what I'm trying for.


template
<typename tTYPE>
void inc(tTYPE& arg)
{ arg++; }

template
<typename tTYPE>
bool inc_dummy(tTYPE& arg)
{
inc(arg);
return false;
}


template
<typename ... vARGS>
void dummy
(vARGS ... V_args)
{}


template
<typename ... vARGS>
void qwert
(vARGS& ... V_args)
{
//inc<vARGS>(V_args)...; // [#1] <-- What I want to do.

//dummy(inc<vARGS>(V_args)...); // [#2] <-- Halfway, but still errors (GCC: invalid use of void expression).

dummy(inc_dummy<vARGS>(V_args)...); // [#3] <-- Current way to do it.
}


int main()
{
int a = 1;
int b = 2;
float c = 3;

qwert(a, b, c);
}


Compiles OK under GCC as is.

This method works, but it's way too complex and untidy. I don't want to have to use dummy functions.

I know I could also use nested template functions, eg:

void qwert() {}

template
<
typename tARG,
typename ... vARGS
>
void qwert
(tARG& arg, vARGS& ... V_args)
{
inc(arg);
qwert(V_args ...);
}


But this still seems overly complex.


Does anyone know a cleaner way? (I'm looking for a language solution, not a library solution like stl or boost unless they do it in a simpler way internally.)



Thanks.

Share this post


Link to post
Share on other sites
I don't think there's a much simpler way than the second.

Your attempts also suffer from the fact that the function calls will occur in an unspecified order, as the arguments to dummy are evaluated.

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
I don't think there's a much simpler way than the second.

OK. Thanks for the confirmation.
I guess the second way is pretty clean especially if you make a meta-template that you only need to write once.

Quote:
Your attempts also suffer from the fact that the function calls will occur in an unspecified order, as the arguments to dummy are evaluated.
Yea, I had noticed that.
Not a problem for my current task but something to remember.

Share this post


Link to post
Share on other sites

This topic is 2586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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