lvalue to rvalue conversion for unordered map

Amber :

Received the parameters from an external library :

candidate function not viable: no known conversion from 'std::unordered_map<string, string>' to 'std::unordered_map<string, string> &&' (aka 'unordered_map<basic_string<char>, basic_string<char> > &&') for 1st argument

void someMethod(shared_ptr<Context> ctx, std::unordered_map<string, string>&& config) {
    ctx->setConfigOverrides(config);
}

// From Context :
// I can't change following method :
void setConfigOverrides(
    std::unordered_map<string, string>&& configOverrides) {
    setConfigOverrides(make_shared<const MemConfig>(move(configOverrides)));
}
WhozCraig :

You're missing moving your input argument on to the next receiver of said-same. This:

ctx->setConfigOverrides(config);

should be this:

ctx->setConfigOverrides(std:move(config));

Proper forwarding requires it. Just because config was passed in as an rval-reference doesn't mean it isn't concrete under the hood. An excellent article on cppreference explains the concept well (not surprising; that site is really amazing, and should be bookmarked if you haven't already). And if you think the syntax is complicated, it gets worse with perfect-forwarding through template deduction. Thankfull, std::forward<> does all the heavy lifting there.

Anyway, that was what was missing. Hope it helps.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=405303&siteId=1