无法从地图获取key_type
unable to get key_type from a map
我写这段代码是为了检查std::map
是否包含特定的键:
template<typename T, typename... Args >
inline bool contains(const std::map<Args...>& map, const T& value) noexcept
{
static_assert(std::is_constructible_v< decltype(map)::key_type , T >);
return map.find(value) != std::end(map);
}
我有以下错误:
错误:
key_type
不是const std::map<std::__cxx11::basic_string<char>, Query>&
的成员
decltype(map)::key_type
有什么问题?
错误非常明确,decltype(map)
const std::map<Args... >&
,这是对std::map
的const
引用。由于它是引用类型,因此它没有::key_type
。
您需要使用std::remove_reference_t
删除引用:
static_assert(std::is_constructible_v<
typename std::remove_reference_t<decltype(map)>::key_type,
T
>);
您需要typename
,因为std::remove_reference_t<decltype(map)>
是依赖名称。
一种更惯用的方法是使用Map
模板参数,而不是将函数限制为std::map
:
template<typename T, typename Map>
inline bool contains(const Map &map, const T& value) noexcept {
static_assert(std::is_constructible_v< typename Map::key_type , T >);
return map.find(value) != std::end(map);
}
相关文章:
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 控制到达非空函数clang(-Wreturn-type)的末尾
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 使用 Key 对 C++ 中的哈希映射进行排序. 无法排序
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- std::<key-value>不同类型的对向量
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- 将内存分配返回值强制转换为 TYPE 数组
- .value( "key" , default) 不适用于空的 json 对象吗?
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- reference_wrapper导致"incomplete type is not allowed"