473,651 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1536
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************ **********@m79g 2000cwm.googleg roups.com>,
rd*****@lycos.c om 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
4293
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. All that I want is to replace node names with a matching value in the lookup table and place the result into an field attribute pair: Example: id to be renamed instrument_id thus <id type="master">asset #132</id> becomes <field...
11
1699
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 flag_type {F1, F2, F3}; } void foo(Flags::flag_type f) {
10
1819
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
2100
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 to these plans??? Stefan
1
3356
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 for editing. I have a table called company that has a field called business model id. That id is a foreign key to the business model lookup table. I've created a strongly typed dataset for company that includes the business model lookup. I bind...
18
2066
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 look up what each feature depends on and gerenate a text file. For example, in the following file, I would like to find out feature A depends on A1 and A2 and feature B depends on B1 and B2. And write that to a text file.
13
5583
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 compile although it appears to me that it should. template<typename TYPE> void foo( std::map< int, TYPE pmap )
4
2432
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 namespace std;
2
432
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
8347
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.