帮助Koenig Lookup [英] Help with Koenig Lookup

查看:80
本文介绍了帮助Koenig Lookup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对参数依赖查找有点困惑。确切的时间

这适用吗?具体来说,我希望用它来访问枚举

常量。例如:


名称空间标志{

enum flag_type {F1,F2,F3};

}


void foo(Flags :: flag_type f)

{

}


int main()

{

foo(F1);

}


我的所有编译器都找不到 ; F1" (他们可能是正确的)。我认为Koenig查找在

参数''类型的名称空间中搜索了名称。现在我想一想,它是否在命名空间中查看正式或实际的参数?


谢谢。

解决方案

REH写道:

我对参数依赖查找感到困惑。究竟什么时候这适用?具体来说,我希望用它来访问枚举
常量。例如:

命名空间标志{
enum flag_type {F1,F2,F3};
}

void foo(Flags :: flag_type f)
{
}
int main()
{foo(F1);


这与依赖于参数的查找无关。 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''找到F1 (他们可能是正确的)。我认为Koenig查找在
参数''类型的名称空间中搜索了名称。


是的。搜索参数的名称空间以查找函数名称。

所以,如果你这样做了


名称空间标志{

enum flag_type {F1,F2,F3};

void foo(Flags :: flag_type f)

{

}

} //名称空间


int main()

{

foo(Flags :: F1); //没有资格''foo''

}


编译器会将''foo''解析为''Flags :: foo'',因为来自Flags命名空间的参数是



现在我想一想,它是否在命名空间中查看正式或实际的参数?




它查找_types_的名称空间(类型是根据

实际参数确定的)。 IOW,如果你做了


Flags :: flag_type someflag;

foo(someflag);


它会仍然将''foo''解析为''Flags :: foo'',因为

''someflag''参数的_type_来自''Flags''命名空间。


V


REH写道:

我对参数依赖查找感到困惑。究竟什么时候这适用?具体来说,我希望用它来访问枚举
常量。例如:

命名空间标志{
enum flag_type {F1,F2,F3};
}

void foo(Flags :: flag_type f)
{
}
int main()
{


foo(Flags :: F1);

}




-

Ioannis Vranos

http://www23.brinkster.com/noicys



" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:EO ******************* @ newsread1.mlpsca01.us.t o.verio.net ... < blockquote class =post_quotes>是的。搜索参数的名称空间以查找函数名称。
所以,如果你这样做

名称空间标志{
enum flag_type {F1,F2,F3};
void foo(Flags :: flag_type f)
{
}
} //名称空间


{/ foo(旗:: F1); //没有''foo'的资格


编译器将''foo''解析为''Flags :: foo'',因为参数是
来自Flags命名空间。



谢谢。这很清楚。我喜欢将我的枚举保存在他们自己的命名空间中,

但总是输入namespace :: constant变得单调乏味。我原本希望使用

Koenig Lookup来避免这种情况,但是从你告诉我的情况来看,我不能(其他

而不是使用)。


问候。


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.

解决方案

REH wrote:

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


REH wrote:

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




--
Ioannis Vranos

http://www23.brinkster.com/noicys



"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:EO*******************@newsread1.mlpsca01.us.t o.verio.net...

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.


这篇关于帮助Koenig Lookup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆