将 unordered_map 与 Catch2 谓词一起使用时类型不匹配
Type mismatch when using unordered_map with Catch2 Predicate
Catch2 给出了一个谓词类来制作我们自己的匹配器。 https://github.com/catchorg/Catch2/blob/master/docs/matchers.md
我只是在这里测试一个unordered_map(decltype(getEntity2IdMap()
(。
namespace Generic {
Predicate<decltype(getEntity2IdMap())>(
[&](auto& maps) -> bool {
return maps.size() == 3 &&
maps["entity1"] == 0 &&
maps["entity2"] == 1 &&
maps["entity3"] == 2; },
"entities were inserted."));
谓词类有一个简单的定义。
template <typename T>
class PredicateMatcher : public MatcherBase<T> {
std::function<bool(T const&)> m_predicate;
std::string m_description;
public:
PredicateMatcher(std::function<bool(T const&)> const& elem, std::string const& descr)
:m_predicate(std::move(elem)),
m_description(Detail::finalizeDescription(descr))
{}
bool match( T const& item ) const override {
return m_predicate(item);
}
std::string describe() const override {
return m_description;
}
};
} // namespace Generic
// The following functions create the actual matcher objects.
// The user has to explicitly specify type to the function, because
// infering std::function<bool(T const&)> is hard (but possible) and
// requires a lot of TMP.
template<typename T>
Generic::PredicateMatcher<T> Predicate(std::function<bool(T const&)> const& predicate, std::string const& description = "") {
return Generic::PredicateMatcher<T>(predicate, description);
}
但是,clang++ 会产生类型不匹配。
error: no viable overloaded operator[] for type 'const std::__1::unordered_map<std::__1::basic_string<char>,
unsigned int, std::__1::hash<std::__1::basic_string<char> >, std::__1::equal_to<std::__1::basic_string<char> >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, unsigned int> > >'
我想知道这里的maps
类型,或者我误解了"/m/entity1"的 lambda 上下文。
完整的错误消息可能是这样的:
<source>:7:21: error: no viable overloaded operator[] for type 'const std::unordered_map<std::string, int>' (aka 'const unordered_map<basic_string<char>, int>')
std::cout << map["test" ] == 1;
~~~^~~~~~~
unordered_map.h:963:7: note: candidate function not viable: 'this' argument has type 'const std::unordered_map<std::string, int>' (aka 'const unordered_map<basic_string<char>, int>'), but method is not marked const
operator[](const key_type& __k)
^
unordered_map.h:967:7: note: candidate function not viable: 'this' argument has type 'const std::unordered_map<std::string, int>' (aka 'const unordered_map<basic_string<char>, int>'), but method is not marked const
operator[](key_type&& __k)
关键线索是'this' argument has type 'const.... but method is not marked const
.
您的地图是常量,但operator[]
不是常量,您需要使用find()
或at()
从常量std::map
或std::unordered_map
中检索值。
相关文章:
- 将 unordered_map 与 Catch2 谓词一起使用时类型不匹配
- 重载函数的地址与所需类型不匹配
- 参数错误可能与类型不匹配有关?
- 调用子例程时类型不匹配
- POSIX(C )参数类型不匹配
- typedef 类型不匹配,将其传递到函数中
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 候选模板被忽略:推断的类型与调整后的类型不匹配
- 内联 ASM:'out'的操作数类型不匹配
- 为什么我们有一个类型不匹配
- C++类和freeglut(参数类型不匹配)
- 警告 639:二进制操作中类型 'uint64' 的强类型不匹配
- 警告 634:相等或有条件的强类型不匹配(类型"bool")
- 不了解C++类型不匹配:const Foo* to Foo* const&
- 函数声明和定义的返回类型不匹配,编译器可以吗?
- 类型不匹配
- 返回指向常量getter的指针时,返回值类型与函数类型不匹配
- llvm pass replaceAllUses与类型不匹配?
- 参数类型不匹配(无效*)
- 重载运算符中的类型不匹配(写入管道)