簡體   English   中英

GNU C ++:為什么自定義類不能是unordered_map的值類型?

[英]GNU C++: why custom class can not be value type for unordered_map?

我有簡單的代碼:

#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編譯器可以毫無問題地編譯這些代碼。 對於具有接受另一個unordered_map實例的構造函數的unordered_map值類型,是否存在任何C ++限制?

這很可能是您正在使用的版本中的stdlibc ++錯誤。

我在ubuntu上使用gcc 4.8.1,並且使用3.5后備箱。 使用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

暫無
暫無

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

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