使用 Z3::expr 作为映射值

Use Z3::expr as a map value

我在使用 Z3::expr 作为地图值类型时遇到了困难。编译器抱怨不能使用默认构造函数。玩具示例如下所示。

#include <z3++.h>
#include <map>
//#include <vector>

using namespace std;
using namespace z3;

int main() {
  map <int, expr> m;
  context c;
  m[1] = c.bool_const("x");
  return 0;
}

编译器 (Clang++) 抱怨

no matching constructor for initialization of 'mapped_type' (aka 'z3::expr')
      __i = insert(__i, value_type(__k, mapped_type()));

in instantiation of member function 'std::map<int, z3::expr, std::less<int>,
std::allocator<std::pair<const int, z3::expr> > >::operator[]' requested here
m[1] = c.bool_const("x");

/usr/include/z3++.h:562:9: note: candidate constructor not viable: requires single argument 'c', but no arguments were provided
    expr(context & c):ast(c) {}
    ^
/usr/include/z3++.h:564:9: note: candidate constructor not viable: requires single argument 'n', but no arguments were provided
    expr(expr const & n):ast(n) {}
    ^
/usr/include/z3++.h:563:9: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
    expr(context & c, Z3_ast n):ast(c, reinterpret_cast<Z3_ast>(n)) {}

使用向量将 expr 或 find 方法包装在 map 中作为替代方法似乎没问题。有没有办法直接用expr作为map值类型+[]运算符?

这是地图的一个普遍问题:[] 实例类型需要一个空的构造函数,请参阅 this and this

(丑陋)调整。 您可能想要派生 map 的子 class,添加对 z3 context 的本地引用,并通过 map_subclass 的构造函数或方法提供此参数。然后,您应该覆盖 [] 以便它使用此引用创建一个新的 expr 实例和构造函数 expr(context &c),而不是试图使用 expr().

我对 z3 内部结构的了解有限,但据我所知这应该不会破坏任何东西。

这个想法很糟糕,因为 z3 是贪婪的,并假设它拥有所有 expressions/objects 的所有权。 不幸的是,您需要使用 z3::expr_vector 并存储偏移位置。

你也有一个越界错误,因为你没有使用正确的 API 插入 map/z3::vector (insert()/push_back()) .