I had the same problem as this previous question here:
Use of undeclared identifier in C++ with templates and inheritance
To sum it up, we try to access a template class’ protected attribute from a child class. The described way to do this would be to use this->attribute instead of only attribute.
The thing is, I was wondering how come visual studio 2012 didn’t need to add the this-> in front of the variable reference for the program to compile and execute properly. I was also wondering if there was a way to have that feature in gcc or other compilers on OS X.
EDIT:
Here is the code I used to test this out in visual studio 2012.
//file a.h
template<class T>
class a
{
public:
a(){value = 2;};
protected:
T value;
};
template<class T>
class b: public a<T>
{
public:
T getValue(){return value;};
};
//file main.cpp
#include <iostream>
#include "a.h"
using namespace std;
int main()
{
b<int> myTest;
cout<<myTest.getValue();
system("pause");
return 0;
}
This doesn’t compile using g++ but does using visual studio 2012.
I believe the part of the standard that describes argument dependent lookup rule applicable in this case is §14.6.2/3, which states the following:
Since your base class depends on a template parameter, the dependent base class scope should not be examined. However, some compilers had this wrong. For example, GCC was doing extra dependent base class scope lookups, which was fixed only in version 4.7 (Bug# 24163, 29131). I do not have an insight as for why Visual Studio compiler allows it. But if it does then it is clearly not standard compliant in this regard. You should not depend on that bug and definitely should not look for compilers with similar bugs to depend on.