Friday, September 08, 2006

Template argument dependent name lookup

Puzzle: What will be the output of the following program?

struct B {
void foo () {
printf ("B::foo");
}
};

void foo () {
printf("::foo");
}

template <typename Base>
struct Derived : public Base {
void bar () { foo (); }
};

int main (void) {
Derived <B> d;
d.bar();

return 0;
}

Answer: C++ compiler does not perform template argument dependent lookup because the rule is to lookup as many names as possible at the time of parsing the template the first time. It does not wait till the instantiation of it. So ::foo!

1 comment:

123 123 said...

Great article you got here. I'd like to read something more concerning that topic. Thank you for posting this information.
Sexy Lady
Escort London