繁体   English   中英

GCC 4.1.1没有用tr1 :: unordered_map编译一些代码

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM