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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:54:49+00:00 2026-06-16T15:54:49+00:00

I have a map containing boost::function values, as defined below: std::map <std::string, boost::function<std::string (std::string,

  • 0

I have a map containing boost::function values, as defined below:

std::map <std::string, boost::function<std::string (std::string, int)> > handlers;

Let us say I define the following function:

using namespace std;
string substring (string input, int index = 0){
    if (index <= 0){
        return input;
    }
    stringstream ss;
    for (int j = index; j<input.length(); j++){
        ss << input[j];
    }

    return ss.str();
}

I would like to be able to store this in the handlers map, but WITH it’s optional parameter. Does boost have a way to perform this? I have looked at boost::optional, but that doesn’t seem to do what I want.

EDIT

To give a little more background, there are a few handlers that require extra arguments, such as a pointer to a dictionary (typedef std::map < std::string, std::string > dictionary) or something, because they make changes to that dictionary. However, the majority of the handlers do not touch the dictionary in question, but, in order to store them all in the same map, they all must take the same arguments (have the same template for boost::function). The goal is to make the functions that don’t deal with the dictionary at all usable without having to either A) create a dictionary for the sole purpose of passing it and not using it or B) copy the code verbatim into another function that doesn’t require that argument.

The code above is a simplified example of what I am doing.

  • 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-16T15:54:50+00:00Added an answer on June 16, 2026 at 3:54 pm

    The short answer: This is not possible in C++ without a lot of additional code.

    The long answer:
    Default values for function arguments in C++ are only used when they are needed in a context where the function’s name appears. If you call a function through other means (like a function pointer, or boost::function/std::function, the information about there possibly being default arguments is not available to the compiler, so it can’t fill them in for you.

    As a background, this is how default arguments work in C++:
    When you have the expression substring(MyString) (with std::string MyString = "something"), then the compiler looks for all functions called substring and finds string substring(string, int=0). This function takes two parameters, one of which can have a default value, which makes the function viable. To actually call the function, the compiler changes the source code so that it reads substring(MyString, 0) and proceeds to generate code based on that adaptation.

    To be able to use default values with an indirect call, like through boost::function, you effectively have to emulate the default argument mechanism of the compiler.

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

Sidebar

Related Questions

i have to use stl map containing 3 values : float(key) , int ,int.
I have a Map containing Strings for keys and Lists as values. After putting
In C++, I have a map<string, string> , containing an unknown number of entries.
I have a class containing Map whose key is string and value is object
Possible Duplicate: Scala map containing mix type values I have a situation where in
I have a function that duplicates a map containing key:function pairs, wrapping each function
If we have a Map<T, Integer> , let's say the Integer value represents how
I'm trying to map a Dictionary containing Lists. I have the following set of
I have a view containing a UIWebView which is loading a google map (so
I have a class containing a std::unique_ptr<> and I want to put instances of

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.