Koening lookup and templates

D

Dilip

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?
 
V

Victor Bazarov

Dilip said:
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
 
D

Dilip

Victor said:
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?
 
V

Victor Bazarov

Dilip said:
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
 
J

Jerry Coffin

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::eek:stream &operator<<(std::eek: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::eek: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top