[英]GCC 4.1.1 doesn't compile some code with tr1::unordered_map
我有以下代碼從一個更大的程序測試一個概念,我嘗試在Linux上使用GCC 4.1.1進行編譯。 由於公司環境的限制,我不能使用更新版本的編譯器,因此我必須編譯並使用當前可用的編譯器版本。
// test.cpp - my tr1::unordered_map usage example
#include <iostream>
#include <tr1/unordered_map>
namespace YY {
class X { public: X(int z_val = 0); private: int z; };
inline X::X(int z_val) : z(z_val) {}
enum XTE { Xt1, Xt2, Xt3 };
}
namespace std { namespace tr1 {
#define _my_tr1_hashtable_define_trivial_hash(T) \
template<> \
struct hash<T> \
: public std::unary_function<T, std::size_t> \
{ \
std::size_t \
operator()(T val) const \
{ return static_cast<std::size_t>(val); } \
}
_my_tr1_hashtable_define_trivial_hash(YY::XTE);
#undef _my_tr1_hashtable_define_trivial_hash
}}
namespace YY {
typedef std::tr1::unordered_map<long long, X*> TXM;
typedef std::tr1::unordered_map<XTE, TXM> TTXM;
}
int main()
{
YY::TTXM m;
std::cout << m.size();
return 0;
}
然后我嘗試編譯這段代碼,gcc給了我以下錯誤:
$ g++ -c test.cpp /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable: In instantiation of Б─≤Internal::hash_code_base, Internal::extract1st >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, false>Б─≥: /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:1014: instantiated from Б─≤std::tr1::hashtable, std::allocator >, Internal::extract1st >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, Internal::prime_rehash_policy, false, false, true>Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/unordered_map:63: instantiated from Б─≤std::tr1::unordered_map, std::equal_to, std::allocator >, false>Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_pair.h:74: instantiated from Б─≤std::pair, std::equal_to, std::allocator >, false> >Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:413: instantiated from Б─≤Internal::extract1st, std::equal_to, std::allocator >, false> > >Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:861: instantiated from Б─≤Internal::hash_code_base, std::equal_to, std::allocator >, false> >, Internal::extract1st, std::equal_to, std::allocator >, false> > >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, false>Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:1014: instantiated from Б─≤std::tr1::hashtable, std::equal_to, std::allocator >, false> >, std::allocator, std::equal_to, std::allocator >, false> > >, Internal::extract1st, std::equal_to, std::allocator >, false> > >, std::equal_to, std::tr1::hash, Internal::mod_range_hashing, Internal::default_ranged_hash, Internal::prime_rehash_policy, false, false, true>Б─≥ /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/unordered_map:63: instantiated from Б─≤std::tr1::unordered_map, std::equal_to, std::allocator >, false>, std::tr1::hash, std::equal_to, std::allocator, std::equal_to, std::allocator >, false> > >, false>Б─≥ test.cpp:42: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/hashtable:863: error: Б─≤Internal::hash_code_base::m_h1Б─≥ has incomplete type /usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/tr1/functional:1101: error: declaration of Б─≤struct std::tr1::hashБ─≥
test.cpp:42:從這里實例化
YY::TTXM m;
如果我改變
typedef std::tr1::unordered_map<XTE, TXM> TTXM;
成
typedef std::tr1::unordered_map<XTE, TXM*> TTXM;
它成功編譯,但這不是我想要做的。 任何想法,建議,如何使這項工作?
它抱怨它long long
沒有哈希函數,所以只需添加
_my_tr1_hashtable_define_trivial_hash(long long);
你應該好好去。
PS我認為使用TXM *而不是TXM工作的原因是因為編譯器不需要解析TXM類型,如果它是一個指針,所以它沒有找到它確定它沒有一切的路徑它需要構造該類型..即長long散列函數。 如果你以后嘗試創建一個TXM對象的實例,它會向你抱怨,然后你會遇到與你現在看到的類似的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.