Robinson
Robinson

Reputation: 10122

Sure I'm doing something very wrong with boost:replace_all. Sanity check please?

I wanted an efficient way to perform a search and replace on a string (actually it's a shader string), so I did a little research and came up with boost::replace_all. My function is passed a string and a vector or pairs. Each pair is a pair of strings; the search and replace with string. I want to iterate this set of replacements, modifying the string in the process. Here's what I came up with:

void 
Shader::Read(ShaderTypes type, std::string const & shader, std::vector<std::pair<std::string, std::string>> const & replace)
{
    // Perform search and replace on input string.

    std::string newShader = shader;

    std::for_each(replace.begin(), replace.end(), [newShader](std::pair<std::string, std::string> const & pair) {
        boost::replace_all(newShader, pair.first, pair.second);
    });




    // Create and compile shader.

    Read(type, newShader);
}

Now this won't compile. I get a slew of errors from boost. I think it's something to do with pair being const, but I'm not 100% certain. When I tried manually creating a std::pair and calling replace_all with it, it worked. Doesn't like the form it arrives from the lambda though.

Can anyone help me please?

Upvotes: 1

Views: 185

Answers (2)

Antoine
Antoine

Reputation: 14084

I'm pretty sure newShader should be passed by reference to the lambda. And you don't have to give its name, you could automatically capture with [&].

Upvotes: 1

Henrik
Henrik

Reputation: 23324

You should capture newShader by reference:

std::for_each(replace.begin(), replace.end(), [&newShader](...

Upvotes: 2

Related Questions