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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:43:56+00:00 2026-05-29T19:43:56+00:00

I try to pass std::log as a functional argument, but it seems that there

  • 0

I try to pass std::log as a functional argument, but it seems that there are overloaded implementations of std::log and the compiler failed to resolve it. Codes:

#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <functional>

template <typename FOper>
double Eval(FOper fOper, double X)
{
    return fOper(X);
}

int main(int argc, char* argv[])
{
    std::function<double(double)> fPlus1 = std::bind(std::plus<double>(), 1.0, std::placeholders::_1);
    std::cout<<Eval(fPlus1, 10.0)<<std::endl;
    //  how to write this fLog ?
    //std::function<double(double)> fLog = std::log;
    //std::function<double(double)> fLog = std::log<double>;
    std::cout<<Eval(fLog, 10.0)<<std::endl;
    return 0;
}

The complier prompts an error message if I uncomment either line of the definition of fLog:

error: conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<double(doubl
e)>' requested
  • 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-29T19:43:58+00:00Added an answer on May 29, 2026 at 7:43 pm

    Like Xeo explained, it is possible to get it to work even when the function is overloaded using an explicit cast. However, since you’re using std::function already (which is a C++11 feature), you might as well just use a lambda expression as initializer:

    function<double(double)> fLog = [](double x){return std::log(x);};
    

    This is preferable in C++11 because it avoids overloading issues. Also, it is more efficient than wrapping a function pointer because it saves one level of indirection and therefore allows the lambda’s body to be inlined into the internal wrapper object’s function call operator.

    It should probably be stressed that the use of std::function in your example is unnecessary since Eval is already a function template and the type parameter FOper can exactly match the type of the function object without the need to wrap it inside std::function. So, if you don’t need the type erasure you get by using std::function, you could just as well write

    template <typename FOper>
    double Eval(FOper fOper, double X)
    {
        return fOper(X);
    }
    
    int main()
    {
        auto flog = [](double x){return std::log(x);};
        std::cout << Eval(flog, 10.0) << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My compiler behaves oddly when I try to pass a fixed-size array to a
I try to pass a NSString from one view to an other view, but
When I try to pass the content of a TextField that contains characters like
What is the difference between ',' and 'as' in except statements, eg: try: pass
If you try to pass the username/password to OpenVPN from a file via the
I'm working on a program where I try to pass parameters by reference. I'm
I have a get-parameter with name controller . When I try to pass it
Given the email address: Jim <jim@example.com> If I try to pass this to MailAddress
I have a class Message that has a std::string as a data member, defined
I have written some C++ code that generates a std::vector. I also have a

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.