簡體   English   中英

以自定義類為鍵的Unordered_map

[英]Unordered_map with custom class as key

我現在正在開發一個編譯器,並且試圖通過從unordered_map繼承來表示作用域類,因為它實際上是符號到聲明的哈希表。 我向該符號添加了一個自定義哈希函數,但是卻收到錯誤消息,抱怨沒有用於初始化std :: pair的默認構造函數。 以下是相關代碼:

Symbol.hpp

#pragma once

#include <string>
#include <unordered_set>


class Symbol
{
    friend class Symbol_table;

    Symbol(std::string const* str) : m_str(str) { }
    /// Constructs the symbol from `str`.

public:
    Symbol() : m_str() { }

    std::string const& str() const { return *m_str; }
    /// Returns the spelling of the token.

    friend bool operator==(Symbol a, Symbol b) 
    {
        return a.m_str == b.m_str;
    }

    friend bool operator!=(Symbol a, Symbol b) 
    {
        return a.m_str != b.m_str;
    }

private:
    std::string const* m_str;  
};


class Symbol_table : std::unordered_set<std::string>
{
public:
    Symbol get(std::string const& str);
    /// Returns the unique symbol for `str`.

    Symbol get(char const* str);
    /// Returns the unique symbol for `str`.
};

inline Symbol
Symbol_table::get(std::string const& str)
{
    return &*emplace(str).first;
}

inline Symbol
Symbol_table::get(char const* str)
{
    return &*emplace(str).first;
}


namespace std
{
    template<>
    struct hash<::Symbol>
    {
        std::size_t operator()(::Symbol sym) const noexcept
        {
            std::hash<std::string const*> h;
            return h(&sym.str());
        }
    };
};

Scope.hpp

#pragma once

#include "decl.hpp"
#include "name.hpp"

#include <string>
#include <vector>
#include <unordered_map>

struct Scope : std::unordered_map<Symbol, Decl*>
{
    Decl* lookup(Symbol sym)
    {
        auto iter = find(sym);
        if (iter == end())
        {
            return nullptr;
        }
        return iter->second;
    }

    void declare(Decl* d)
    {
        assert(!already_declared(d));
        emplace(d->get_name()->get_str(), d);
    }

    bool already_declared(Decl* d)
    {
        return d->get_name() != nullptr;
    }
};

struct Scope_stack : std::vector<Scope>
{
    Decl* lookup(Symbol sym)
    {
        for (auto iter = rbegin(); iter != rend(); ++iter)
        {
            if (Decl * d = iter->lookup(sym))
            {
                return d;
            }
        }
        return nullptr;
    }
};

這是編譯錯誤:

/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1805:31: error: no matching constructor for initialization of
      'std::__1::pair<const Symbol, Decl *>'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在這個pastebin中 ,我在此錯誤下面還有很多內容。

emplace(d->get_name()->get_str(), d);

在這里,您嘗試從const std::string&Decl*構造std::unordered_map<Symbol, Decl*>::value_type (即std::pair<Symbol, Decl*> )。

問題在於Symbol沒有構造函數采用std::string ,而只有std::string const*

這是錯誤消息中提供提示的行:

 /Library/Developer/CommandLineTools/usr/include/c++/v1/utility:422:5: note: candidate constructor not viable: no known conversion from 'std::__1::basic_string<char>' to 'const const Symbol' for 1st argument pair(_T1 const& __t1, _T2 const& __t2) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM