下面是一个示例代码,其中 std::hash 类型的模板参数是将要被包装的类型,struct Wrapper 创建一个包装器 wrapped, 其中 operator() 函数返回传递给 std::hash 函数的哈希值。
#include
template
struct Wrapper {
T wrapped;
size_t operator()(const T& value) const {
return std::hash()(value);
}
};
int main() {
Wrapper wrapped{42};
auto hash_value = wrapped(42);
return 0;
}
上一篇:包装函数助手