Using boost::recursive_variant with std::unordered_map

I’m trying to create a std::unordered_map with a boost::variant as a key and with integer value. What I’m trying to make work is this:

#include <boost/variant.hpp>

#include <unordered_map>
#include <string>

enum Token {};

struct AssignExpr;
struct LiteralExpr;

using Expr = boost::variant<boost::blank, 

using Literal = int;

struct LiteralExpr {
  Literal literal;

  explicit LiteralExpr(const Literal &literal);

struct AssignExpr {
  Token name;
  Expr value;

  AssignExpr(const Token &name, const Expr &val);

int main() {
  LiteralExpr li{0};
  AssignExpr ae{{}, li};

  std::unordered_map<Expr, std::size_t> m_locals{{li, 10}, {ae, 20}};

I tried to add operator== and an hash function but get a linkage error.

>Solution :

You put this in the declaration of your struct:

explicit LiteralExpr(const Literal &literal);

But you never provided an implementation for it. What is it supposed to do?

Also, for future reference, please include any error messages in the question itself. Links rot.

Leave a Reply