SonarLint's warning about using "transparent function objects" with associative containers like std::unordered_map<std::string, ...> relates to optimizing key lookups to avoid constructing temporary std::string objects. Transparent comparators or hash functions allow you to use types other than the container's key type for lookups, minimizing unnecessary allocations and improving performance.
In C++, associative containers such as std::unordered_map typically use key comparison or hashing functions when looking up elements. By default, these functions work only with the exact key type (std::string in your case). When you enable transparency with transparent function objects, the associative container can perform these operations directly on compatible types without converting them first.
For std::unordered_map<std::string, ...>, this can be achieved by specifying the hash and key equality comparators as transparently operable, using std::hash<std::string>, std::equal_to<>, or similar, which support transparent lookups. However, the standard std::hash<std::string> does not natively support transparency (because hash functions for fundamental types like const char* need custom handling), so it's more common to see this in conjunction with ordered containers (like std::map with std::less<>).
To address the warning, you would typically define custom hash and equality functions that are marked "transparent." Here's a basic example:
#include <iostream>
#include <unordered_map>
#include <variant>
#include <string>
// Define a transparent hash function
struct TransparentHash {
using is_transparent = void; // Marks hash as transparent
std::size_t operator()(const std::string& s) const {
return std::hash<std::string>{}(s);
}
std::size_t operator()(std::string_view sv) const {
return std::hash<std::string_view>{}(sv);
}
std::size_t operator()(const char* s) const {
return std::hash<std::string_view>{}(s);
}
};
// Define a transparent equality function
struct TransparentEqual {
using is_transparent = void; // Marks equal_function as transparent
bool operator()(const std::string& lhs, const std::string& rhs) const {
return lhs == rhs;
}
bool operator()(std::string_view sv, const std::string& rhs) const {
return sv == rhs;
}
bool operator()(const char* lhs, const std::string& rhs) const {
return std::string_view(lhs) == rhs;
}
};
// Use ParamType as defined
using ParamType = std::variant<int, double, std::string>;
int main() {
std::unordered_map<std::string, ParamType, TransparentHash, TransparentEqual> parameters;
parameters["pi"] = 3.14159;
// Lookup using different types without constructing std::string
std::string pi_key = "pi";
std::cout << "pi: " << std::get<double>(parameters.at(pi_key)) << '\n';
std::cout << "pi: " << std::get<double>(parameters.at("pi")) << '\n';
return 0;
}
In this code snippet, I've defined TransparentHash and TransparentEqual to enable transparent lookup using std::string, std::string_view, and const char*. These types can be used in lookups without the need to first convert them to std::string, avoiding unnecessary memory allocations and copies. This is particularly useful in performance-sensitive applications where such optimizations are beneficial.