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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:15:25+00:00 2026-05-29T04:15:25+00:00

I have such template functions: template<class R> list<R> f(const boost::function<R()>&); template<class R, class A0>

  • 0

I have such template functions:

template<class R> list<R> f(const boost::function<R()>&);
template<class R, class A0> list<R> f(const boost::function<R(T0)>&, list<A0>);
template<class R, class A0, class A1> list<R> f(const boost::function<R(T0)>&, list<A0>, list<A1>);

To run one of them i need to write for example:

int one() { return 1; }
int inc(int x) { return x + 1; }

list<int> l;

f<int>(one);
f<int, int>(inc, l);

And my goal is to just write:

f(one);
f(inc, l);

I heard that this is possible by some kind of template signature specialization, but I can’t figure out how.

  • 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-29T04:15:25+00:00Added an answer on May 29, 2026 at 4:15 am

    Without C++11, you can’t get away from specifying a return type of a function.

    template<typename R, typename F>
        R bar(F func) {
            return func();
        }
    
    bar<int>(foo);
    

    With the new C++11 features you can though.

    template<typename F>
        auto baz(F func) -> decltype(func()) {
            return func();
        }
    
    baz(foo);
    

    You can template the function/functor as a parameter, instead of trying to specify that it has to be a boost::function.

    void zero() {cout << "zero" << endl;}
    void one(int a) {cout << "one" << endl;}
    void two(int a, int b) {cout << "two" << endl;}
    
    template<typename F>
        void f(const F &func) {
            func();
        }
    
    template<typename F, typename T0>
        void f(const F &func, T0 t0) {
            func(t0);
        }
    
    template<typename F, typename T0, typename T1>
        void f(const F &func, T0 t0, T1 t1) {
            func(t0, t1);
        }
    

    This lets you pass in the function pointer rather simply.

    f(zero);
    f(one, 1);
    f(two, 1, 2);
    

    If you need to actually use functions or bind you can pass that in to the same interface.

    // without specifying the function
    f(boost::bind(zero));
    f(boost::bind(one, _1), 1);
    f(boost::bind(two, _1, _2), 1, 2);
    
    // or by specifying the object
    boost::function<void()> f0        = boost::bind(zero);
    boost::function<void(int)> f1     = boost::bind(one, _1);
    boost::function<void(int,int)> f2 = boost::bind(two, _1, _2);
    f(f0);
    f(f1, 1);
    f(f2, 1, 2);
    

    As well as with a functor, which is typical for passing in strict weak ordering behavior to the standard containers.

    struct zoobies {
        void operator()() const {}
    };
    
    f(zoobies());
    

    It doesn’t have to check the type of what you pass in to it, only that it satisfies the interface. This is one of the reasons C++ templates are typically much more powerful than generics in other languages.

    And for completeness… If you actually did want to restrict it to boost::function, here is an example.

    template<typename T>
        void p(const boost::function<T> &func) {
            func();
        }
    
    template<typename T, typename A0>
        void p(const boost::function<T> &func, A0 a0) {
            func(a0);
        }
    
    
    boost::function<void()> f0(zero);
    p(f0);
    
    boost::function<void(int)> f1(one, _1);
    p(f1, 1);
    

    Update:

    void foo() {cout << "zero" << endl;}
    void foo(int a) {cout << "one" << endl;}
    void foo(int a, int b) {cout << "two" << endl;}
    

    boost::bind works out of the box, with this, although the raw function pointers have more of a problem. foo is ambiguous there.

    f( (void(*)())        foo      );
    f( (void(*)(int))     foo, 1   );
    f( (void(*)(int,int)) foo, 1, 2);
    

    If you fully specify the function pointer then it works, though that isn’t what anyone wants to do.

    With boost::bind as evidence, You should be able to determine the arity from the calling convention of f. If I get some time today I’ll play with it.

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

Sidebar

Related Questions

I have a member function of a template class declared as such: template <class
I have such html and css. <div class=selected> <div class=text>First</div> <div class=arrow>&nbsp;</div> </div> .selected
I have such a class: public class Cycle { public List<int> Edges { get;
I have the following functions in class C class C { template<typename T> void
Suppose I have such a template class: template <class T> class Queue { public:
I have a class with several template member functions that I would like to
Given a set of functions, such as: template<class A1> Void Go(A1 a); template<class A1,
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have such class public unsafe class EigenSolver { public double* aPtr {get; private

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.