简体   繁体   English

C ++模板类继承

[英]C++ template class inheritance

I have been porting some C++ code, which was written long time ago, and is normally compiled with Visual C++ (Visual Studio 7.1 version) and Intel C++ Compiler 11.0, the target platform is Linux (Suse x86-64), with GCC 4.3.2 and Intel C++ Compiler 11.1 我一直在移植一些C ++代码,这些代码是很久以前编写的,通常使用Visual C ++(Visual Studio 7.1版)和Intel C ++ Compiler 11.0进行编译,目标平台是Linux(Suse x86-64)和GCC 4.3。 2和Intel C ++编译器11.1

The problem is that code like this 问题是这样的代码

FileA.h 文件A.h

template<typename T, int dim>
class A
{
 public:
  A(){};
  ~A(){};
 protected:
  void foo1(){};
}

FileB.h 文件库

#include "FileA.h"
template<typename T>
class B : public A<T, 2>
{
 public:
  B(){};
  ~B(){};
  void foo(){ foo1(); }
}

main.cpp main.cpp

#include "FileB.h"
int main()
{
 B<float> b = B<float>();
}

does not compile on Linux (Intel C++ 11.1, GCC 4.3.2), but perfectly compiles on Windows (Visual C++ 7.1, Intel C++ 11.0), althow it surely must not depend on platform. 不能在Linux(Intel C ++ 11.1,GCC 4.3.2)上编译,但是可以在Windows(Visual C ++ 7.1,Intel C ++ 11.0)上完美编译,因此它一定不能依赖于平台。 GCC tells that if I change foo1() to foo1(T a) it will work (and it does), but I can not change the code, and have to use Intel C++ for final release. GCC告诉我,如果将foo1()更改为foo1(T a),它将可以工作(并且可以),但是我无法更改代码,必须使用Intel C ++进行最终发行。

I would be glad if anyone could help with any advice. 如果有人可以提供任何建议,我将非常高兴。

foo1 is not a dependent expression so the base class, which is a dependent type, is not used to resolve the foo1 call. foo1不是依赖表达式,因此不使用基类(即依赖类型)来解析foo1调用。

As you can't change the code, you are stuffed. 由于您无法更改代码,因此已被塞满。 If you could change the code you would need to change the expression to be dependent. 如果可以更改代码,则需要将表达式更改为从属。 Typically this is done by changing it to this->foo1() . 通常,这是通过将其更改为this->foo1()

This is a well-known problem with templates. 这是模板的一个众所周知的问题。 It is explained in the C++ FAQ C ++常见问题解答中有解释

On gcc 4.4.1 (os is Ubuntu) version I could turn the compile error into a compile warning, by using the -fpermissive option to the compiler. 在gcc 4.4.1(操作系统为Ubuntu)版本上,可以通过使用-fpermissive选项将编译错误转换为编译警告。

Edit: The fact that some compiler accept it, doesn't mean it will continue to accept it in future versions. 编辑:某些编译器接受它的事实,并不意味着它将在将来的版本中继续接受它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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