Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:39+00:00 2026-05-26T16:41:39+00:00

I want to do identical processing to a bunch of arguments of a function.

  • 0

I want to do identical processing to a bunch of arguments of a function. Is there a way to loop over all arguments ? I am doing it the way represented in following code, but want to see if there is a compact way to do this.,

 void methodA(int a1, int a2, int b1, double b2){   
        //.. some code 
        methodB(a1, f(a1));
        methodB(a2, f(a2));
        methodB(b1, f(b1));
        methodB(b2, f(b2));
        // more code follows ...

   }

int f(int a){
      // some function. 
   return a*10;
}

double  f(double b){
   return b/2.0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:41:40+00:00Added an answer on May 26, 2026 at 4:41 pm

    You could use variadic templates:

    template <typename T, typename ...Args>
    void methodAHelper(T && t, Args &&... args)
    {
      methodB(t, f(t));
      methodAHelper(std::forward<Args>(args)...);
    }
    
    void methodAHelper() { }
    
    template <typename ...Args>
    void methodA(Args &&... args)
    {
      // some code
      methodAHelper(std::forward<Args>(args)...);
      // some other code
    }
    

    You can possibly get rid of the && and the forwarding if you know that your methodB call doesn’t know about rvalue references, that would make the code a bit simpler (you’d have const Args &... instead), for example:

    methodAHelper(const T & t, const Args &... args)
    {
      methodB(t, f(t));
      methodAHelper(args...);
    }
    

    You might also consider changing methodB: Since the second argument is a function of the first argument, you might be able to only pass the first argument and perform the call to f() inside the methodB(). That reduces coupling and interdependence; for example, the entire declaration of f would only need to be known to the implementation of methodB. But that’s depends on your actual situation.

    Alternatively, if there is only one overload of methodB whose first argument is of type T, then you could just pass a std::vector<T> to methodA and iterate over it:

    void methodA(const std::vector<T> & v)
    {
      // some code
      for (auto it = v.cbegin(), end = v.cend(); it != end; ++it)
        methodB(*it, f(*it));
      // some more code
    }
    
    int main() { methodA(std::vector<int>{1,2,3,4}); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple classes, all of which I want to send an identical message
I want to add few lines of identical html code to several pages in
Is there an easy way of cloning entire installed debian/ubuntu system? I want to
I want to having all common data (i.e. nationalities) identical in all site (the
I want to tell whether two tarball files contain identical files, in terms of
Want the function to sort the table by HP but if duplicate HPs then
Want to code a key pad for an calculator. What I want to make
want to ask user to input something but not want to wait forever. There
For example, for accessibility reasons I want my onfocus and onmouseover to have identical
I have some divs with identical structure and i want to toggle when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.