5

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.

Community
  • 1
  • 1
Sam
  • 794
  • 1
  • 7
  • 26

1 Answers1

4

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:

In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

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.

  • That's pretty interesting. Maybe microsoft (that's just speculation on my part) is not correcting that problem for ease of use? – Sam Jan 26 '13 at 07:09
  • @Sam: I don't know why they are not correcting it. However, ease of use is one thing, and doing what programmer expects is another. One can reference a parameter with the same name and assume that compiler picks it up from global scope, and Microsoft will surprise the developer with its ease of use non-standard behavior. MS actually has a long track record of surprising developers with its C++ compiler... I don't use their products for years, but I reckon in version 6 they used to call a constructor of an object inside the loop scope only once. –  Jan 26 '13 at 07:13
  • inside the loop scope, you mean you have a while loop for example using multiple times a constructor and it would call the constructor only once? – Sam Jan 26 '13 at 08:45
  • @Sam: Exactly. But that was a long, long time ago, like in my other life. On the other hand, all compilers have bugs :) –  Jan 26 '13 at 18:23
  • I was just scratching my head over this one. It's been a while since I've programmed in C++ and I forgot about this lovely hairy little corner case. This language sure isn't friendly. – Eloff Oct 25 '15 at 01:59