C++ 无序映射在添加我的自定义哈希函数时需要复制构造函数

问题描述 投票:0回答:1

我正在尝试为类

A
使用自定义哈希结构,这是
std::unordered_map
中的键类型,它是
std::variant
类中的
B
替代方案之一。 这是我的代码的简化版本,可以在其中重现错误:

#include <initializer_list>
#include <string>
#include <unordered_map>
#include <variant>

namespace myNamespace {
    class A;
    struct AHasher;
    class B;

    // ....

    class A {
        public:
            A(const std::string& str);
            friend bool operator==(const A& lhs, const A& rhs);
        public:
            std::string value;
    };

    struct AHasher {
        std::size_t operator()(const A& str) const;
    };

    class B {
        public:
            B();
            B(const std::string& str);
            B(const A& str);
            B(const std::initializer_list<std::pair<const A, B>> list);
        private:
            std::variant<A, std::unordered_map<A, B, AHasher>> value;
    };

}  // namespace myNamespace 

// .......

namespace myNamespace {
    A::A(const std::string& str) : value(str) {}
    bool operator==(const A& lhs, const A& rhs) { 
        return lhs.value == rhs.value;
    }

    std::size_t AHasher::operator()(const A& str) const {
        std::hash<std::string> hasher;
        return hasher(str.value);
    }
   
    B::B() : value(std::unordered_map<A, B, AHasher>{}) {}
    B::B(const std::string& str) : value(A(str)) {}
    B::B(const A& str) : value(str) {}
    B::B(const std::initializer_list<std::pair<const A, B>> list) : value(list) {}

} // namespace myNamespace 


int main() {
    return 0;
}

我正在使用 GCC 9.4.0 进行编译,命令为:

g++ -std=c++17 -Wall -Wextra -pedantic -O0 main.cpp
编译失败并显示以下消息:

In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/string:40,
                 from tmp.cpp:53:
/usr/include/c++/9/bits/stl_pair.h: In instantiation of ‘struct std::pair<const myNamespace::A, myNamespace::B>’:
/usr/include/c++/9/ext/aligned_buffer.h:91:28:   required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const myNamespace::A, myNamespace::B> >’
/usr/include/c++/9/bits/hashtable_policy.h:233:43:   required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const myNamespace::A, myNamespace::B> >’
/usr/include/c++/9/bits/hashtable_policy.h:264:12:   required from ‘struct std::__detail::_Hash_node<std::pair<const myNamespace::A, myNamespace::B>, true>’
/usr/include/c++/9/bits/hashtable_policy.h:2027:13:   required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const myNamespace::A, myNamespace::B>, true> > >’
/usr/include/c++/9/bits/hashtable.h:173:11:   required from ‘class std::_Hashtable<myNamespace::A, std::pair<const myNamespace::A, myNamespace::B>, std::allocator<std::pair<const myNamespace::A, myNamespace::B> >, std::__detail::_Select1st, std::equal_to<myNamespace::A>, myNamespace::AHasher, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’
/usr/include/c++/9/bits/unordered_map.h:105:18:   [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/9/type_traits:901:12:   required from ‘struct std::__is_copy_constructible_impl<std::unordered_map<myNamespace::A, myNamespace::B, myNamespace::AHasher>, true>’
/usr/include/c++/9/type_traits:907:12:   required from ‘struct std::is_copy_constructible<std::unordered_map<myNamespace::A, myNamespace::B, myNamespace::AHasher> >’
/usr/include/c++/9/type_traits:2918:25:   required from ‘constexpr const bool std::is_copy_constructible_v<std::unordered_map<myNamespace::A, myNamespace::B, myNamespace::AHasher> >’
/usr/include/c++/9/variant:275:5:   required from ‘constexpr const bool std::__detail::__variant::_Traits<myNamespace::A, std::unordered_map<myNamespace::A, myNamespace::B, myNamespace::AHasher, std::equal_to<myNamespace::A>, std::allocator<std::pair<const myNamespace::A, myNamespace::B> > > >::_S_copy_ctor’
/usr/include/c++/9/variant:1228:11:   required from ‘class std::variant<myNamespace::A, std::unordered_map<myNamespace::A, myNamespace::B, myNamespace::AHasher, std::equal_to<myNamespace::A>, std::allocator<std::pair<const myNamespace::A, myNamespace::B> > > >’
tmp.cpp:81:64:   required from here
/usr/include/c++/9/bits/stl_pair.h:215:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
  215 |       _T2 second;                /// @c second is a copy of the second object
      |           ^~~~~~
tmp.cpp:74:11: note: forward declaration of ‘class myNamespace::B’
   74 |     class B {
      |           ^

为什么我会在这里收到有关复制构造函数的消息?据我了解,如果我没有明确地创建自己的构造函数,编译器将生成默认的复制和移动构造函数

在使用此实现之前,我没有使用

std::unordered_map
,而是在类
std::map
中使用具有适当运算符重载的
A
,一切正常

c++ hash c++17 copy-constructor unordered-map
1个回答
3
投票

提到复制构造函数是转移注意力。生成其中一个拷贝构造函数时出现错误,但错误与拷贝构造函数无关:

错误:‘std::pair<_T1, _T2>::second’ 的类型不完整

错误类型不完整。您的

B
包含一个
std::unordered_map
,它本身包含
B
B
的定义直到它的右大括号才完成;因此
B
,当声明无序映射不完整时,它是一个前向声明。

C++标准的各种修订版本对于哪些容器可以定义为容器值的不完整类存在差异,只有在实际使用时才需要定义。那是一个单独的问题,但是就您关于复制构造函数与其中任何一个有什么关系的问题而言,答案是它没有。

© www.soinside.com 2019 - 2024. All rights reserved.