就像標題所暗示的,我有一個簡短的演示程序,可以編譯所有這些編譯器,但在使用gcc 4.8和gcc 4.9編譯後運行核心轉儲:爲什麼Clang和VS2013接受移動大括號初始化的默認參數,但不接受GCC 4.8或4.9?
有關爲什麼?
#include <unordered_map>
struct Foo : std::unordered_map<int,int> {
using std::unordered_map<int, int>::unordered_map;
// ~Foo() = default; // adding this allows it to work
};
struct Bar {
Bar(Foo f = {}) : _f(std::move(f)) {}
// using any of the following constructors fixes the problem:
// Bar(Foo f = Foo()) : _f(std::move(f)) {}
// Bar(Foo f = {}) : _f(f) {}
Foo _f;
};
int main() {
Bar b;
// the following code works as expected
// Foo f1 = {};
// Foo f2 = std::move(f1);
}
我的編譯設置:
g++ --std=c++11 main.cpp
下面是GDB回溯:
#0 0x00007fff95d50866 in __pthread_kill()
#1 0x00007fff90ba435c in pthread_kill()
#2 0x00007fff8e7d1bba in abort()
#3 0x00007fff9682e093 in free()
#4 0x0000000100002108 in __gnu_cxx::new_allocator<std::__detail::_Hash_node_base*>::deallocate()
#5 0x0000000100001e7d in std::allocator_traits<std::allocator<std::__detail::_Hash_node_base*> >::deallocate()
#6 0x0000000100001adc in std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<int const, int>, false> > >::_M_deallocate_buckets()
#7 0x000000010000182e in std::_Hashtable<int, std::pair<int const, int>, std::allocator<std::pair<int const, int> >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::_M_deallocate_buckets()
#8 0x000000010000155a in std::_Hashtable<int, std::pair<int const, int>, std::allocator<std::pair<int const, int> >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::~_Hashtable()
#9 0x000000010000135c in std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<int const, int> > >::~unordered_map()
#10 0x00000001000013de in Foo::~Foo()
#11 0x0000000100001482 in Bar::~Bar()
#12 0x0000000100001294 in main()
*** error for object 0x1003038a0: pointer being freed was not allocated ***
你在gcc上使用了哪些標誌? – ThomasMcLeod
爲什麼在使用這個構造函數時會發生:'Bar(Foo f = {Foo()}):_f(std :: move(f)){} – ThomasMcLeod
它的核心轉儲,原因相同。 – vmrob