问题描述
尝试此操作:
template <typename T>
class Base
{
public:
int someBaseMember;
};
template <typename T>
class Test: public Base<T>
{
public:
void testFunc()
{
someBaseMember = 0;
}
};
在vc ++和psp编译器(以及遇到的任何其他编译器) ,用iphone编译器(设备,gcc 4.2我想,与-fpermissive标志设置)我得到一个错误说
'someBaseMember没有定义'
在
'someBaseMember = 0 ;'
行
In vc++ and the psp compiler (and any other compiler I've encountered) the above will work fine, with the iphone compiler (for device, gcc 4.2 I think, with the -fpermissive flag set) I get an error saying'someBaseMember is not defined'on the'someBaseMember = 0;'line
iphone编译器似乎比其他编译器更快地'解析'模板化的代码,甚至没有语法检查他们,直到你实际CALL函数,或实例化一个实例。)
The iphone compiler seems to be 'parsing' templated code a lot sooner than other compilers do, (from what I can tell, most others don't even syntax check them until you actually CALL the function, or instantiate an instance.)
从我可以告诉它很快,它的解析它没有
From what I can tell its parsing it so soon that it hasn't even parsed the base class yet :S its like it doesn't exist.
任何想法?
推荐答案
您得到的错误是正确的(其他编译器不应该接受代码,并且这样错误);变量 someBaseMember
取决于 Base< T>
的模板实例化, ,因此编译器在尝试独立于模板参数进行解析时是正确的。
The error that you are getting is correct (the other compilers should not accept the code and are doing so erroneously); the variable someBaseMember
depends on the template instantation of Base<T>
, but this dependence has not been expressed in your usage, and hence the compiler is correct in attempting to resolve it independently of the template parameter.
您可以通过使此依赖性显式来解决此问题,从而强制编译器解析该变量使用模板实例化。您可以使用以下任意一种:
You can resolve this problem by making this dependence explicit, thereby forcing the compiler to resolve the variable using the template instantation. You can use either of the following:
this-> someBaseMember = 0;
或
Base< T> :: someBaseMember = 0;
上述两种情况都会导致您想要的解析机制。
Either of the above should result in the resolution mechanism you want.
EDIT
您可能希望看到C ++ FAQ Lite的相关部分:
这篇关于iphone编译器继承的模板基类与传递通过类型不扩展的时间(只是看)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!