Help with Koenig Lookup

R

REH

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)
{
}

int main()
{
foo(F1);
}

All my compilers fail to find "F1" (and they are probably correct). I
thought that Koenig lookup searched for names in the namespaces of the
arguments' types. Now that I think about it, does it look in the namespaces
for the formal or actual arguments?

Thanks.
 
V

Victor Bazarov

REH said:
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)
{
}

int main()
{
foo(F1);

This has nothing to do with argument-dependent lookup. 'F1' is simply
not present in the scope of the 'main' function.
}

All my compilers fail to find "F1" (and they are probably correct). I
thought that Koenig lookup searched for names in the namespaces of the
arguments' types.

Yes. The namespaces of the arguments are searched for the function names.
So, if you do

namespace Flags {
enum flag_type {F1, F2, F3};
void foo(Flags::flag_type f)
{
}
} // namespace

int main()
{
foo(Flags::F1); // no qualification for 'foo'
}

The compiler will resolve 'foo' as 'Flags::foo' because the argument is
from the Flags namespace.
Now that I think about it, does it look in the namespaces
for the formal or actual arguments?

It looks in the namespaces of the _types_ (types are determined from the
actual arguments of course). IOW, if you did

Flags::flag_type someflag;
foo(someflag);

it would still resolve 'foo' as 'Flags::foo' because the _type_ of the
'someflag' argument is from 'Flags' namespace.

V
 
I

Ioannis Vranos

REH said:
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)
{
}

int main()
{
foo(Flags::F1);

}
 
R

REH

Victor Bazarov said:
Yes. The namespaces of the arguments are searched for the function names.
So, if you do

namespace Flags {
enum flag_type {F1, F2, F3};
void foo(Flags::flag_type f)
{
}
} // namespace

int main()
{
foo(Flags::F1); // no qualification for 'foo'
}

The compiler will resolve 'foo' as 'Flags::foo' because the argument is
from the Flags namespace.
Thank you. That's clear. I like to keep my enums in their own namespace,
but always typing "namespace::constant" gets tedious. I had hoped to use
Koenig Lookup to avoid that, but from what you are telling me I can't (other
than with "using").

Regards.
 
I

Ioannis Vranos

REH said:
Thank you. That's clear. I like to keep my enums in their own namespace,
but always typing "namespace::constant" gets tedious. I had hoped to use
Koenig Lookup to avoid that, but from what you are telling me I can't (other
than with "using").

All *names* specified in a namespace must be used with their namespace
name (either explicit namespace::name style or via using statements).
 
V

Victor Bazarov

Ioannis said:
All *names* specified in a namespace must be used with their namespace
name (either explicit namespace::name style or via using statements).

No, that's incorrect. See my example:

namespace NS {
class foo;
void bar(foo*);
}

int main() {
NS::foo* pfoo = 0;
bar(pfoo);
}

I happily used 'bar' without a namespace. That's what ADL is all about.

V
 
R

REH

Victor Bazarov said:
No, that's incorrect. See my example:

namespace NS {
class foo;
void bar(foo*);
}

int main() {
NS::foo* pfoo = 0;
bar(pfoo);
}

I happily used 'bar' without a namespace. That's what ADL is all about.

V

But, I take it that there is no way to do this with non-function names?
 
V

Victor Bazarov

REH said:
But, I take it that there is no way to do this with non-function names?

Right. _Argument_-dependent lookup applies only to function names. The
only other thing that has arguments in C++ is templates. But ADL doesn't
apply to them:

namespace NS {
enum foo { f };
template<foo f> class bar {};
}

int main() {
bar<NS::f> barf; // line 7
}

Here, on line 7, 'bar' is not going to be looked up in 'NS' even though
its _argument_ is fully qualified (and found in 'NS' namespace).

V
 
I

Ioannis Vranos

Victor said:
No, that's incorrect. See my example:

namespace NS {
class foo;
void bar(foo*);
}

int main() {
NS::foo* pfoo = 0;
bar(pfoo);
}

I happily used 'bar' without a namespace. That's what ADL is all about.


Where is the function definition?
 
I

Ioannis Vranos

Ioannis said:
Where is the function definition?


Apart from missing function definition, this is the case of ADL (for
example bar(0); would not compile).
 
V

Victor Bazarov

In another translation unit.
Apart from missing function definition, this is the case of ADL (for
example bar(0); would not compile).

Of course it wouldn't. 0 has type 'int' there is no bar(int) here and the
NS namespace is not going to be looked at if you simply pass 0. But this:

bar((NS::foo*)0)

would compile just as well. Again, I didn't have to use 'NS::bar' for it
to be found. ADL takes care of that.

V
 
I

Ioannis Vranos

Victor said:
Of course it wouldn't. 0 has type 'int' there is no bar(int) here and the
NS namespace is not going to be looked at if you simply pass 0.


Even if it was void bar(int) inside NS, it would not be called.

But this:

bar((NS::foo*)0)

would compile just as well. Again, I didn't have to use 'NS::bar' for it
to be found. ADL takes care of that.


Yes, it finds it because of NS::foo use. In general, you have to use the
namespace name in some way.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top