C++ GNU C++;:为什么自定义类不能是无序映射的值类型?
我有简单的代码:C++ GNU C++;:为什么自定义类不能是无序映射的值类型?,c++,gcc,unordered-map,C++,Gcc,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 &o
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
甲级
{
公众:
A(无效)
{
}
A(常数A和其他)
{
}
A运算符=(常量A和其他)
{
归还*这个;
}
A(常量无序图和值)
{
}
};
内部主(空)
{
A A;
无序地图m;
printf(“你好,世界!”);
返回0;
}
由于任何原因,GNU C++ 4.4.2不能编译它,并显示了很多错误;我看到它“不喜欢”构造函数
A(const unordered\u map&value)
。原因是什么?MSVC编译器编译这样的代码没有问题。在“代码>未排序的映射,< /代码>值类型中是否有C++限制,它具有接受另一个<代码>未排序的映射实例的构造函数? < p>这是您正在使用的版本中最可能的一个STDLBC+++错误。
我在ubuntu上使用GCC4.8.1和Clang3.5 trunk。使用libc++的clang没有问题。使用libstdc++的clang和gcc会触发问题
对于以下更简单的主函数,您的类型会触发相同的问题:
int main() {
using namespace std;
cout << is_convertible<const A &, A>::value << endl;
}
你能把代码简化成一个简单的测试用例吗?不可能从这样的描述中猜出什么是错误的。据我从错误中猜测,
GoodGuySoft::Dynamic
没有您描述的复制构造函数。@根据我毫无根据的经验,通常是MSVC是有缺陷的编译器和/或具有扩展,允许编译在gcc中无法编译的代码。考虑到这很可能是代码中的一个错误(因为您能够减少它,而不是重现错误),那么就没有理由同时标记这两个编译器了。所以可能是bug,谁知道呢。使用GCC 4.8.2命令g++-std=c++11 build/test.cpp-o../obj/test.o
时,该代码会出现很多错误。如果您希望看到它处于活动状态(断开)。我相信ideone.com也使用4.8.2,并且。如果您还不知道的话,删除无序的_映射引用构造函数可以让它通过。我在这里使用clang 3.5 trunk和gcc 4.8.1进行编译,这似乎是库的问题。使用libc++使用clang可以很好地编译代码,但它不使用clang进行编译,也不使用libstdc++使用gcc进行编译。
/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