Curiously Recurring Template Pattern Bug in VS2010?

131 views Asked by At

Why would this code not compile in VS2010?

struct Base
{
    void foo0() { }
};
template<typename BASE> struct Derived : BASE
{
    void foo1() { foo0(); }
};
int main()
{
    Derived<Base> ddd;
    ddd.foo1();
    return 0;   
}

It compiles just fine in other compilers. It also compiles if I call foo0 with the help of this: this->foo0();

2

There are 2 answers

3
orlin On

I just figured out that the reason behind this behavior is Language Extensions compiler option (/Za). So if Language Extensions are disabled, above code would not compile. What is strange here is, this must be part of the C++ language and not the MS Language Extensions.

Hope this helps someone...

1
Yakk - Adam Nevraumont On

At the point of declaration of Derived<BASE>::foo1(), it does not know what BASE is. It then tries to figure out what foo0 is. There is no foo0 in view -- or worse it finds a global foo0 to call. This is an error.

When you instead call this->foo0() you inform the compiler that you want to find it as a method.