I created a following piece of code just to show what I am trying to do. I do not understand how to properly use std::forward
, so the code may not work properly. I have a std::map<std::string, std::string>
named myMap
. The key is a string-path similar to the file-paths used in unix based OSes. The key
specified to the insertValue function can start with a dot indicating relative path. The condition below replaces the relative path with absolute path. Now to the question part...
How to properly implement the following function? Here are my concerns:
- Will it allow to pass all kind of arguments i.e. for move and for copy?
- If move occurs, can I do the following:
key = getAbsolutePath(key);
- Do I need to use template for the
Key
andValue
or can I just use thestd::string
?
Edit 1: Here is the code edited based on comments:
template <typename Key, typename Value>
void insertValue(Key &&key, Value &&value)
{
auto absoluteKey = isRelativePath(key) ? getAbsolutePath(std::forward<Key>(key)) : std::forward<Key>(key);
myMap.insert_or_assign(std::move(absoluteKey), std::forward<Value>(value));
}
Edit 2:
For those who want also use efficiently the getAbsolutePath
here is example code:
template <typename T>
std::decay_t<T> getAbsolutePath(T &&path)
{
std::string prefix = "/example/base/path/";
return prefix + std::forward<T>(path);
}