Formatting formatting using boost bind for a method that uses only one parameter

I am trying to use boost :: regex_replace using custom formatting. I need to pass a method from an object because some member is required in the replacement function.

Signature of my replacement method:

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

When called, regex_replaceI pass the following arguments:

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

The problem is that regex_replace calls the format method as a result of the match used by Functor - it is one that accepts 3 parameters (Custom formatting can be string, unary, binary or triple). I think this is due to the fact that boost :: bind somewhat hides arity functions.

The reason I think this is because arity disappears is because when linked to

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

(, ).

, , , , , , boost:: bind - , .

, boost bind.

EDIT: , , boost::bind - . , , :

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;
}
+3
1

boost::, :

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);
+2

Source: https://habr.com/ru/post/1774341/


All Articles