簡體   English   中英

std :: make_pair,std :: unordered_map以及鍵類型中的move構造函數的用法

[英]std::make_pair, std::unordered_map and usage of move constructor from key type

給出以下結構:

struct FieldNo
{
    FieldNo() : a('0'), b('0') {}
    FieldNo(char a_, char b_) : a(a_), b(_b) {}
    // copy construction and assigment not allowed
    FieldNo(const FieldNo& other) = delete;
    FieldNo& operator=(const FieldNo& other) = delete;
    // move construction and assignment ok
    FieldNo(FieldNo&& other) = default;
    FieldNo& operator=(FieldNo&& other) = default; 

    char a;
    char b;
};

enum class Members : int8_t
{
    FOO,
    BAR
};

我正在使用FieldNo作為鍵,將Member作為std :: unordered_map的值。 省略哈希創建函數的代碼,我的地圖定義如下:

typedef std::unordered_map<FieldNo, Members, FieldNoHasher> MyMapT;

后來我用下面的方法初始化並返回到調用者

const MyMapT& map()
{
   static const MyMapT fields = 
   {
       std::make_pair(FieldNo('0', '5'), Members::FOO),
       std::make_pair(FieldNo('1', 'X'), Members::BAR)
   }
   return fields;
}

最初,復制構造函數未刪除,並且一切正常。

當我刪除要復制的構造函數時,我在插入過程中遇到了很多錯誤,並將最相關的錯誤放在這里:

[build] 
[build] /usr/include/c++/8/ext/new_allocator.h:136:4: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const FieldNo; _T2 = Members]’
[build]   { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
[build]     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
[build]                  from /usr/include/c++/8/bits/char_traits.h:39,
[build]                  from /usr/include/c++/8/ios:40,
[build]                  from /usr/include/c++/8/istream:38,
[build]                  from /usr/include/c++/8/fstream:38,
[build]                  from 
[build] /usr/include/c++/8/bits/stl_pair.h:303:17: note: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const FieldNo; _T2 = Members]’ is implicitly deleted because the default definition would be ill-formed:
[build]        constexpr pair(const pair&) = default;
[build]                  ^~~~
[build] /usr/include/c++/8/bits/stl_pair.h:303:17: error: use of deleted function ‘FieldNo::FieldNo(const libtraco::FieldNo&)’

我知道錯誤是由於缺少副本構造函數而引起的。 但是,為什么還要嘗試復制內容呢? std::make_pair FieldNo('0', '5')顯然是一個rvalue 我想念什么嗎?

謝謝!

編輯:添加了最小的可復制示例

#include <unordered_map>

struct FieldNo
{
    FieldNo() : a('0'), b('0') {}
    FieldNo(char a_, char b_) : a(a_), b(b_) {}
    // copy construction and assigment not allowed
    FieldNo(const FieldNo& other) = delete;
    FieldNo& operator=(const FieldNo& other) = delete;
    // move construction and assignment ok
    FieldNo(FieldNo&& other) = default;
    FieldNo& operator=(FieldNo&& other) = default;

    bool operator==(const FieldNo& lhs) const
    {
        return a == lhs.a && b == lhs.b;
    }

    char a;
    char b;
};

template <class T>
inline void hash_combine(std::size_t& seed, const T& v)
{
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}

struct FieldNoHasher 
{
    std::size_t operator()(const FieldNo& input) const
    {
        std::size_t seed = 0;
        hash_combine(seed, input.a);
        hash_combine(seed, input.b);
        return seed;
    }
};

enum class Members : int8_t
{
    FOO,
    BAR
};

typedef std::unordered_map<FieldNo, Members, FieldNoHasher> MyMapT;

const MyMapT& map()
{
   static const MyMapT fields = 
   {
       std::make_pair(FieldNo('0', '5'), Members::FOO),
       std::make_pair(FieldNo('1', 'X'), Members::BAR)
   };

   return fields;
}

int main()
{
    map();
}

這是一個最小的可重現示例:

#include <unordered_map>
#include <utility>

class C {
public:
    C() = default;
    C(const C&) = delete;
    C& operator=(const C&) = delete;
    C(C&&) = default;
    C& operator=(C&&) = default;
};

bool operator==(const C&, const C&)
{
    return true;
}

struct Hash {
    constexpr std::size_t operator()(const C&) const
    {
        return 42;
    }
};

int main()
{
    std::unordered_map<C, int, Hash> map {
        std::make_pair(C{}, 0)
    };
}

現場演示

問題在於您正在調用initializer_list構造函數。 由於的方式initializer_list的作品,一個的底層元素initializer_listconst ,這意味着元素只能被復制,不移動。 如果您的類型是僅移動,則不能使用initializer_list構造函數。 您必須使用其他設施:

std::unordered_map<C, int, Hash> map;
map.emplace(C{}, 0);

或者,您也可以用另一個支持移動的容器(例如,向量)替換initializer_list

std::vector<std::pair<const C, int>> values {
    std::make_pair(C{}, 0)
};
std::unordered_map<C, int, Hash> map(values.cbegin(), values.cend());

現場演示

暫無
暫無

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

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