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 9251609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:44:44+00:00 2026-06-18T10:44:44+00:00

I have a question continuing the post Function passed as template argument . In

  • 0

I have a question continuing the post Function passed as template argument. In the provided code:

#include <iostream>

void add1(int &v)
{
  v+=1;
}

void add2(int &v)
{
  v+=2;
}

template <void (*T)(int &)>
void doOperation()
{
  int temp=0;
  T(temp);
  std::cout << "Result is " << temp << std::endl;
}

int main()
{
  doOperation<add1>();
  doOperation<add2>();
}

what about a third function which has a different parameter set layout, e.g.

double add3(double v1, double v2)
{
return v1+v2;
}

If this is not achievable using template at all, how do we pass an arbitrary function to another function? And how do we handle the parameter set with all kinds of possibilities? I know python may be able to do it by passing a tuple (kwargs**), but not sure about C/C++.

  • 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-06-18T10:44:45+00:00Added an answer on June 18, 2026 at 10:44 am

    One form of passing a generic function to be called is a callable templated type:

    #include <functional>
    #include <iostream>
    
    template<typename F>
    void callFoo(F f) {
       f();
    }
    
    int main() {
       callFoo(std::bind([](int a, int b) {std::cout << a << ' ' << b;}, 5, 6));
    }
    

    callFoo takes a callable type, F, and calls it. Around this call, you can, for example, do timer work to time the function. In main, it’s called with a lambda that has two parameters and the values given to those parameters bound to it. callFoo can then call it without storing the arguments. This is very similar to taking a parameter with the type std::function<void()>.

    If, however, you don’t want to use std::bind, you can pass in the arguments separately with a couple changes:

    template<typename F, typename... Args>
    void callFoo(F f, Args... args) { //ignoring perfect forwarding
        f(args...);
    }
    
    int main() {
        callFoo(/*lambda*/, 5, 6);
    }
    

    In these cases, passing void functions makes sense. Indeed, return values can be used as parameters and passed in with std::ref. If you plan on returning what the function returns, you’ll have to handle the special case of the return type being void, as you can’t assign to a void variable and return that. At this point, it’s easier to direct you to my previous question on the matter. My use case for it turned out to be moot, but the solution works great for other uses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question i.e how can i select a previous div CODE:- if($i >
I have a general question. I would like to have a window containing some
Ok simple question. I have a JSF application, containing a login page. The problem
I am working with VB.NET.. i have a DataTable called QUESTION, containing 3 fields:
I have question about parsing in Html helper : I have sth like: @foreach
I have question. I have some app on facebook and getting this error Fatal
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have Question and QuestionTypes. In Question table there is a foreign key that
I have asked a similar question before and got an convincing answer as well?

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.