By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,742 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,742 IT Pros & Developers. It's quick & easy.

Koening lookup and templates

P: n/a

How did the Koenig lookup come to be associated with templates?

If I have something like this: (no template code)

namespace X
{
enum E { e1 };
void f(E) { }
}

void f(int)
{
}

int main()
{
f(X::e1);
return 0;
}

does the call in main invoke X::f() or ::f()?
In the text I am reading it says the former is preferred but how does
Koening Lookup assume precedence over ordinary lookup?

Aug 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dilip wrote:
How did the Koenig lookup come to be associated with templates?

If I have something like this: (no template code)

namespace X
{
enum E { e1 };
void f(E) { }
}

void f(int)
{
}

int main()
{
f(X::e1);
return 0;
}

does the call in main invoke X::f() or ::f()?
Should invoke X::f().
In the text I am reading it says the former is preferred but how does
Koening Lookup assume precedence over ordinary lookup?
The name 'f' is unqualified. So, 3.4.2 governs the lookup. According to
it, other namespaces may be searched. So, they probably are. So, X::f is
*added* to the list of overloaded functions. Then one needs to be picked.
No conversion required for X::f() and that's how it would be preferred.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #2

P: n/a
Victor Bazarov wrote:
Dilip wrote:
In the text I am reading it says the former is preferred but how does
Koening Lookup assume precedence over ordinary lookup?

The name 'f' is unqualified. So, 3.4.2 governs the lookup. According to
it, other namespaces may be searched. So, they probably are. So, X::f is
*added* to the list of overloaded functions. Then one needs to be picked.
No conversion required for X::f() and that's how it would be preferred.
Impressive, as always. Thanks!

So to be clear I am not really talking about Koening lookup, am I?
This is just following lookup rules for unqualifed names, right? Was
ADL (I assuming that is what Koening lookup is actually called as)
invented to circumvent lookup problems after C++ templates were
introduced? If so how did the previous snippet function before ADL was
introduced?

Aug 9 '06 #3

P: n/a
Dilip wrote:
Victor Bazarov wrote:
>Dilip wrote:
>>In the text I am reading it says the former is preferred but how
does Koening Lookup assume precedence over ordinary lookup?

The name 'f' is unqualified. So, 3.4.2 governs the lookup.
According to it, other namespaces may be searched. So, they
probably are. So, X::f is *added* to the list of overloaded
functions. Then one needs to be picked. No conversion required for
X::f() and that's how it would be preferred.

Impressive, as always. Thanks!

So to be clear I am not really talking about Koening lookup, am I?
Yes, ADL is what's known as Koenig lookup.
This is just following lookup rules for unqualifed names, right?
Well, right...
Was
ADL (I assuming that is what Koening lookup is actually called as)
invented to circumvent lookup problems after C++ templates were
introduced?
I am not sure, let's how Andrew will chime in, but I would suspect
that it was actually introduced to make sure that 'std' namespace
defined functions are actually found, even if they are not qualified.
If so how did the previous snippet function before ADL
was introduced?
Frankly, I don't remember. I wasn't paying attention then...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #4

P: n/a
In article <11**********************@m79g2000cwm.googlegroups .com>,
rd*****@lycos.com says...
Victor Bazarov wrote:
Dilip wrote:
In the text I am reading it says the former is preferred but how does
Koening Lookup assume precedence over ordinary lookup?
The name 'f' is unqualified. So, 3.4.2 governs the lookup. According to
it, other namespaces may be searched. So, they probably are. So, X::f is
*added* to the list of overloaded functions. Then one needs to be picked.
No conversion required for X::f() and that's how it would be preferred.

Impressive, as always. Thanks!

So to be clear I am not really talking about Koening lookup, am I?
Yes, you are.
This is just following lookup rules for unqualifed names, right?
Koenig lookup is one of the lookup rules for unqualified names.
Specifically when you're looking up the unqualified name of a function,
the namespace(s) of its parameter(s) are in the list of places to look.
Was
ADL (I assuming that is what Koening lookup is actually called as)
invented to circumvent lookup problems after C++ templates were
introduced?
If memory serves, ADL was related more closely to the introduction of
namespaces than of templates. While it's convenient for some functions,
it's (mostly) crucial when you use overloaded operators -- the proper
overload of the operator will should normally be in the same namespace
as the type:

namesspace Z {
class X {
};

int f(X const &) {}

std::ostream &operator<<(std::ostream &, X const &) {
};
};

int main() {
Z::X x;

f(x); // ADL finds Z::f()

Z::f(x); // easily done without ADL though.

// not so easy to handle without ADL:
std::cout << x; // no easy way to indicate Z::operator<<

return 0;
}
If so how did the previous snippet function before ADL was introduced?
Without ADL, the function inside of the namespace would never have even
been considered so the one in the global namespace would have been used.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.