Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Koening lookup and templates

Reply
Thread Tools

Koening lookup and templates

 
 
Dilip
Guest
Posts: n/a
 
      08-09-2006

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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2006
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


 
Reply With Quote
 
 
 
 
Dilip
Guest
Posts: n/a
 
      08-09-2006
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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2006
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


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      08-10-2006
In article < .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:stream &operator<<(std:stream &, 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:perator<<

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
templates, namespace, and name lookup Stefan Naewe C++ 0 08-19-2008 10:18 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Class templates and friend function templates BigMan C++ 1 07-23-2005 09:24 PM
Templates: "implicit typename is deprecated" error and typedef'ing templates Generic Usenet Account C++ 3 07-14-2005 08:02 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57