http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46283
Summary: Strange lookup of p->type::foo expressions
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
This prints 1, thinking that 'foo' means a::foo:
#include <stdio.h>
struct b {
typedef struct a foo;
int x() { return 1; }
void go();
};
struct a : public b {
typedef b foo;
int x() { return 2; }
};
void b::go() { printf("%i\n", ((a *)0)->foo::x()); }
int main() {
b().go();
}
If I trivially parametrise b on the derived type T and turn the cast to a *
into a cast to T *, it starts printing 2 as it starts finding the foo from
local name lookup:
#include <stdio.h>
template <typename T>
struct b {
typedef struct a foo;
int x() { return 1; }
void go();
};
struct a : public b<a> {
typedef b<a> foo;
int x() { return 2; }
};
template <typename T>
void b<T>::go() { printf("%i\n", ((T *)0)->foo::x()); }
int main() {
b<a>().go();
}
This happens with every version of gcc I've tried (4.1 through 4.5). Other
compilers error on this code, complaining that they find different foo doing
name lookup in a or T and doing a normal local name lookup. I hear the
standard agrees with that behaviour.