低版本GCC程序向高版本移植的兼容性问题

将低版本gcc编译过的程序移植到高版本GCC时, 可能会出现一些兼容性问题. 原因是, 为了适应新的标准,一些旧的语法规则被废弃了. 关于这方面的一些具体资料可从该处查询. 这里只是自己遇到的其中一个问题.

错误提示:

In instantiation of ‘int t(T) [with T = int]’
  required from here
  error: ‘f’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
  note: ‘int f(int)’ declared here, later in the translation unit

错误原因:

 The C++ compiler no longer performs some extra unqualified lookups it had performed in the past, namely dependent base class scope lookups and unqualified template function lookups.

C++ programs that depended on the compiler's previous behavior may no longer compile. For example, code such as

//foo1.cpp
template<typename T> int t(T i) { return f(i); } int f(int i) { return i; } int main() { return t(1); }

解决方法:

To fix, make sure the function f in the code above is declared before first use in function t. Like so:

//foo2.cpp
int
f(int i) { return i; } template<typename T> int t(T i) { return f(i); } int main() { return t(1); }

意思是说, C++编译器不再支持那种非标准的模板类中函数查找方式,如foo1.cpp所示, 在定义模板类时使用的函数在该模板类之前未定义, 而是在该模板类之后定义. 早期的C++编译器支持向后查找. 但是新标准出来后, 该方式被废除了. 因此会报错. 如foo2.cpp所示, 在模板类中使用某个函数之前应先对该函数进行定义. 这才是正确的姿势.

 Or this can be temporarily worked around by using -fpermissive.

第一种方法方法可以从根本上解决问题, 但是有情况是我们编译别人的程序, 并且程序十分复杂, 不是我们可以轻易改变的. 这时有个临时性的方法, 那便是在编译时添加 -fpermissive 选项. 我自己的项目是利用Cmake进行构建的, 因此只需要在 CmakeList.txt 文件中添加一行: ADD_DEFINITIONS(-fpermissive), 然后重新 cmake 一下即可. 此时将错误降为警告, 可顺利通过编译.

原文地址:https://www.cnblogs.com/bozhicheng/p/5925008.html