GNU C++:为什么自定义类不能作为unordered_map的值类型?

3

我有一段简单的代码:

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

using namespace std;

class A
{
public:
    A(void)
    {
    }
    A(const A &other)
    {
    }
    A &operator=(const A &other)
    {
        return *this;
    }
    A(const unordered_map<int, A> &value)
    {
    }
};

int main(void)
{
    A a;
    unordered_map<wstring, A> m;
    printf("Hello, World!");
    return 0;
}

由于某种原因,GNU C++ 4.8.2无法编译它并显示了许多错误;我发现它“不喜欢”构造函数A(const unordered_map<int, A> &value)。这是什么原因?MSVC编译器可以毫无问题地编译此类代码。是否存在C ++对接受另一个unordered_map实例的构造函数具有构造函数的值类型的限制?


6
能否将代码缩减为一个简单的测试用例?仅从描述中无法猜测出可能出现的错误。从错误信息可以猜测,GoodGuySoft::Dynamic 没有你所描述的复制构造函数。 - Mike Seymour
1
根据我的经验,通常是MSVC编译器出现错误,或者使用了扩展使得在gcc中无法编译的代码得以编译。考虑到最有可能是你的代码有问题(因为你已经减少代码并且未能重现该错误),所以没有必要同时标记两种编译器。@VASoftOnline - user1508519
1
可能是个bug,谁知道呢。使用GCC 4.8.2命令g++ -std=c++11 build/test.cpp -o ../obj/test.o编译该代码会产生很多错误。 - Vitalii
1
如果您想要看到它的实际效果(崩溃),我相信ideone.com也使用4.8.2版本,这里是它所有悲剧的展示。如果您还不知道,删除unordered_map引用构造函数可以使其通过测试。 - WhozCraig
1
嗯,我刚刚注意到其中一个错误抱怨不完整的类型,因为你的构造函数。如果你将它改为 A(const unordered_map<int, A*> &value) {},那么它就可以编译通过了。 - user1508519
显示剩余14条评论
1个回答

1
这很可能是一个stdlibc++的bug,与您使用的版本有关。我在Ubuntu上使用gcc 4.8.1和clang 3.5 trunk。使用libc++的clang没有问题。对于使用libstdc++的clang和gcc,触发了该问题。对于以下更简单的main函数,对于您的类型也会触发相同的问题:
int main() {
    using namespace std;
    cout << is_convertible<const A &, A>::value << endl;
}

此外,从使用std::unordered_map改为使用std::map,没有任何问题。
这是更加清晰易懂的clang错误日志:
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:117:14: error: base class has incomplete type
    : public conditional<_B1::value, _B2, _B1>::type
      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_pair.h:122:19: note: in instantiation of template class
      'std::__and_<std::is_convertible<const int &, const int>, std::is_convertible<const A &, A> >' requested here
               enable_if<__and_<is_convertible<const _U1&, _T1>,
                         ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_pair.h:124:12: note: in instantiation of default argument for
      'pair<const int, A>' required here
        constexpr pair(const pair<_U1, _U2>& __p)
                  ^~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_pair.h:124:12: note: while substituting deduced template arguments into
      function template 'pair' [with _U1 = const int, _U2 = A, $2 = <no value>]
        constexpr pair(const pair<_U1, _U2>& __p)
                  ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:803:24: note: in instantiation of default argument for
      '__test<std::pair<const int, A>, const std::pair<const int, A> &>' required here
      static true_type __test(int);
                       ^~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:803:24: note: while substituting deduced template arguments into
      function template '__test' [with _Tp = std::pair<const int, A>, _Arg = const std::pair<const int, A> &, $2 = <no value>]
      static true_type __test(int);
                       ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:117:14: note: (skipping 10 contexts in backtrace; use
      -ftemplate-backtrace-limit=0 to see all)
    : public conditional<_B1::value, _B2, _B1>::type
             ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/unordered_map.h:97:27: note: in instantiation of template type alias
      '__check_copy_constructible' requested here
    class unordered_map : __check_copy_constructible<_Alloc>
                          ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1305:42: note: in instantiation of template class
      'std::unordered_map<int, A, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, A> > >' requested here
        static decltype(__test_aux<_To1>(std::declval<_From1>()), __one())
                                         ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1306:2: note: while substituting explicitly-specified template
      arguments into function template '__test'
        __test(int);
        ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1319:11: note: in instantiation of template class
      'std::__is_convertible_helper<const A &, A, false>' requested here
                               __is_convertible_helper<_From, _To>::value>
                               ^
c++1y-sample.cpp:33:13: note: in instantiation of template class 'std::is_convertible<const A &, A>' requested here
    cout << is_convertible<const A &, A>::value << endl;
            ^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:1317:12: note: definition of 'std::is_convertible<const A &, A>' is
      not complete until the closing '}'
    struct is_convertible

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接