473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Koening lookup and templates


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
4 1521
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Adrian Charteris | last post by:
Hi I'm currently trying to use a lookup table for converting one xml doc to another using a XSLT transformation. Ideally I would like my first xml doc to be converted to my second xml doc below. ...
11
by: REH | last post by:
I'm a little confused about argument dependent lookup. Exactly when does this apply? Specifically, I was hoping to use it to access enumeration constants. For example: namespace Flags { enum...
10
by: philchen1978 | last post by:
Hi, I can compile the code below with GCC 3.4.2, because function g is a "dependent name". template<class T> void f1(T t) { g(t); }
29
by: Stefan Slapeta | last post by:
There have been many announciations that VC 8.0 will finally support two phase template lookup; now, after I installing VC 2005 Express I had to realize that it still doesn't! What has happend...
1
by: Big Dave | last post by:
Good morning. This group has been a great help so far, and it is much appreciated. Here's my new question. I want to be able to create a datagrid at runtime, which will have template columns...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
13
by: paul.joseph.davis | last post by:
Hi, I've just had my first encounter with two-phase lookup and I'm scratching my head a bit. The idea behind two phase look up is pretty easy to understand, but I have a case that fails to...
4
by: siddhu | last post by:
Dear experts, As far as I understood the ADL concept things should work in the following way. I have a global ostream operator defined as //ostreamtest.cpp #include<iostream> using...
2
by: Jim West | last post by:
Can someone please explain to me why the following compiles: class A { public: int d; }; class B : public A { public: int e;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.