c++ - 如何修复 Google OR-Tools 中的 C++03 双尖括号模板语法错误?

标签 c++ c++11 or-tools

我正在尝试构建一个 Google OR-Tools OSX 10.11.3 上的示例

当我运行时:

g++  -I../include/ -L../lib -lortools -Wno-c++11-extensions nqueens2.cc

In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
In file included from ../include/base/hash.h:19:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/hash_map:212:5: warning: Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map> [-W#warnings]
#   warning Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>
    ^
In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
In file included from ../include/base/hash.h:20:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/hash_set:205:5: warning: Use of the header <ext/hash_set> is deprecated.  Migrate to <unordered_set> [-W#warnings]
#   warning Use of the header <ext/hash_set> is deprecated.  Migrate to <unordered_set>
    ^
In file included from nqueens2.cc:25:
In file included from ../include/constraint_solver/constraint_solver.h:65:
../include/base/hash.h:184:31: error: expected expression
struct hash<std::array<T, N>> {
                              ^
../include/base/hash.h:201:2: error: expected a type
};
 ^
2 warnings and 2 errors generated.

我认为问题在于编译器包含 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ext/hash_map 当我希望它从 ../include 中包含它时。

导致错误的代码来自 ../include/base/hash.h(Google OR-Tools 附带):

183 template <class T, std::size_t N>
184 struct hash<std::array<T, N>> {
185  public:
186   size_t operator()(const std::array<T, N>& t) const {
187     uint64 current = 71;
188     for (int index = 0; index < N; ++index) {
189       const T& elem = t[index];
190       const uint64 new_hash = hash<T>()(elem);
191       current = operations_research::Hash64NumWithSeed(current, new_hash);
192     }
193     return current;
194   }
195   // Less than operator for MSVC.
196   bool operator()(const std::array<T, N>& a, const std::array<T, N>& b) const {
197     return a < b;
198   }
199   static const size_t bucket_size = 4;  // These are required by MSVC
200   static const size_t min_buckets = 8;  // 4 and 8 are defaults.
201 };
202 #endif  // STLPORT
203 }  // namespace HASH_NAMESPACE

有什么想法吗?

最佳答案

旧的 g++ 编译器不允许连续两个 > 用于模板声明,因为它显示为 >> 运算符。因此,当您的代码查看结构声明时,它看到的是 { 大括号,而不是 >>> 运算符的表达式。这在后来的 c++ 标准中有所更改,g++ 也相应更改。

C++03 编译器要求将 >> 解释为右移运算符。这在 C++0x 标准中有所改变。

关于c++ - 如何修复 Google OR-Tools 中的 C++03 双尖括号模板语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35419183/

相关文章:

c++ - 为什么使用引用限定符 & 声明删除的赋值运算符

c++ - 在 C++ 中使用 reinterpret_cast 向下转型

c++ - 推力转换抛出错误 : "bulk_kernel_by_value: an illegal memory access was encountered"

C++ 无法翻转 ppm 图像

c++ - 如何在一组迭代器上调用 `find`,通过迭代器指向的内容进行查找?

c++ - 带有 size_t 模板参数的函数

c++ - OpenMP 任务等待不工作

python - 在线性问题中定义多个约束后,获取变量当前可能接受的界限

c# - 员工轮类问题 - 将任务联系在一起

python - 使用 OR 工具在 python 中进行约束优化 : How to enforce a multi level constraint?