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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:04:21+00:00 2026-05-18T02:04:21+00:00

I’m trying to use boost::regex_replace with a custom formatter. I need to pass a

  • 0

I’m trying to use boost::regex_replace with a custom formatter. I need to pass a method from an object because some member is needed in the replacing function.

The signature of my replacing method is:

std::string MyObject::ReplaceStr(
    boost::match_results<std::string::const_iterator> match) const

When calling regex_replace, I pass these arguments:

std::string replaced = regex_replace(
    orig_string, replace_pattern, 
    boost::bind<std::string>(&MyObject::ReplaceStr, this, _1));

The problem is when regex_replace calls the format method on the match result, the Functor used is the one taking 3 parameters(Custom formatter can be string, unary, binary or ternary function). I think it is due to the fact that boost::bind somewhat hides arity of a function.

The reason why I think it is due the the arity disappearing is because when binding with

std::string replaced = regex_replace(
    orig_string, replace_pattern,       
    std::bind1st(std::mem_fun(&MyObject::ReplaceStr), this));

the right functor(the one using the unary function) is called.

I could also probably just use a ternary function in my object to bind to and then it would probably work but for the sake of understanding and using boost::bind can someone explain if I understood correctly and if not provide the right explanation.

Bonus point if I can make it work with boost bind.

EDIT: I forgot to tell that it crashes when I use boost::bind due to selecting the wrong method signature. Here’s a code snippet to reproduce the behavior I tried to explain:

using namespace std;
using namespace boost;

class MyObject
{
public:
    void ReplacePattern()
    {
        const std::string testString = "${value_to_replace}extra_value";

        boost::regex replace_pattern("(\\$\\{(.*?)\\})");
        std::string replaced = regex_replace(testString, replace_pattern, boost::bind(&MyObject::ReplaceStr, this, _1));

        cout << "Replaced: " << replaced << endl;
    }

    std::string ReplaceStr(
        boost::match_results<std::string::const_iterator> match) const
    {
        return "replaced_value";
    }
};


int main(int argc, char* argv[])
{
    MyObject obj;
    obj.ReplacePattern();


    char dummy[1];
    cin.getline(dummy, 1);

    return 0;
}
  • 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-18T02:04:21+00:00Added an answer on May 18, 2026 at 2:04 am

    You could use boost::function to avoid ambiguity:

    boost::function<std::string (boost::match_results<std::string::const_iterator>)> function =
        boost::bind(&MyObject::ReplaceStr, this, _1);
    std::string replaced = regex_replace(testString, replace_pattern, function);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.