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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:10:56+00:00 2026-05-31T20:10:56+00:00

A C++11 template function which takes a function object argument, such as: template <typename

  • 0

A C++11 template function which takes a function object argument, such as:

template <typename F, typename T>
auto foo(F f, T x) -> decltype(f(x)) {
  return f(x);
}

could take a function such as:

int succ(int i) { return i+1; }

and apply foo to succ and obtain a result of 10:

foo(succ,9);

Overloaded functions don’t work, sin, for example, fails:

foo(std::sin,0.5);

in GCC (4.7) with “couldn’t deduce template parameter F”.

(Providing sin<double> relates only to complex types btw.) Yes, I can struct it right up:

template <typename T>
struct Sin {
  T operator()(T x) { return std::sin(x); }
};

with a little:

foo(Sin<double>(),0.5);

My question is, is there an alternative which avoids the need for such a new definition; usable solely at the call site of foo?

  • 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-31T20:10:57+00:00Added an answer on May 31, 2026 at 8:10 pm

    For function pointers, you can simply have the user type the signature:

    template<class F, class T>
    void foo(F f, T x){
      f(x);
    }
    
    void bar(int){}
    void bar(double){}
    
    int main(){
      foo<void(int)>(bar, 5);
    }
    

    Live example on Ideone.

    foo will be void foo(void f(int), int x) after substitution, which is the same as foo(void (*f)(int), int x). This provides a so-called “calling context” which allows the compiler to select the correct overload. Obviously, this only works well if the first template parameter is the function. To work around this limitation, and make it look nicer (imho, atleast), you can provide a simple helper function:

    template<class F>
    auto get_overload(F f) -> decltype(f) { return f; }
    

    Actually, you can only overload on parameter types, but this you can’t cull out the need to have the user type the return type, since this disables the calling context again, as a type would need to be deduced.

    Since you most likely (or surely) only want this for function pointers, you can change it to this:

    template<class F>
    F* get_overload(F* f){ return f; }
    

    It’s still completely the same. The only reason why the first version can’t just have F as the return type is that F is void(int) if you call it with get_overload<void(int)>(bar), and the standard doesn’t allow you to return functions (yes, that’s a function type). The function to function pointer transformation (void(int) -> void(*)(int)) part only happens for parameters.


    Since for whatever reason @VJovic deleted his answer, I’ll just edit this in:

    You can actually use a simple lambda instead of the get_overload function. It will be about the same length character wise and much more convenient and clearer. It will also be more efficient, since no (function) pointers are involved and the compiler is perfectly able to inline the call.

    foo([](int i){ return bar(i); }, 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote a function template which takes a reference to a C-array: template
How do I go about writing a function which takes an object of a
Is it possible to write a c++ template function which takes a variable number
Suppose I have a function which looks like this: template <class In, class In2>
I'm passing to Django's template a function, which returns some records. I want to
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
I have function which takes in an parameter of a class called Triple, and
I have a function which takes three arguments void replace(const string, const string, string*)

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.