为什么koenig查找? [英] Why koenig lookup?

查看:69
本文介绍了为什么koenig查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所谓的koenig查找允许做这样的奇怪事情:


#include< algorithm>

#include< string>


int main()

{

std :: string table [10];

sort( table,table + 10);

}


(请注意''sort''如何没有std ::前缀,但它's
完全有效。)


我明白koenig查询存在的原因是

你可写类似的东西:


std :: cout<< 5;


这实际上相当于:


运算符<<(std :: cout,5);


koenig查找允许编译器查找运算符<<内部

std命名空间,因为其中一个参数来自该命名空间。


使用运算符是有意义的。但是,为什么对* all *函数执行相同的查找

?有什么好处?难道不能简单地定义

为操作员执行koenig查找而不是常规的

函数吗?

解决方案

Juha Nieminen写道:


所谓的koenig查找允许做这样奇怪的事情:


#include< algorithm>

#include< string>


int main()

{

std :: string table [10];

sort(table,table + 10);

}


(请注意''sort''如何没有std ::前缀,但是它'

完全有效。)


我明白koenig查询存在的原因是

你可以写下这样的东西:


std :: cout<< ; 5;


这实际上相当于:


运算符<<(std :: cout,5);


koenig查找允许编译器查找运算符<<内部

std命名空间,因为其中一个参数来自该命名空间。


使用运算符是有意义的。但是,为什么对* all *函数执行相同的查找

?有什么好处?难道不能简单地定义

为操作员执行koenig查找而不是常规的

函数吗?



它可以节省大量的打字,我认为这就是为什么它也适用于

功能。如果函数在命名空间std中有效,为什么

应该一直输入它。如果你不想使用std,那就不要使用命名空间std;"
"在代码的开头。编译器

将一直要求std ::


如果你键入行,编译器不需要std :: any,

,因为你已经在命名空间std。


Captain Trips写道:


嗯,它节省了很多打字,我认为这就是为什么它也适用于

功能。如果函数在命名空间std中有效,为什么

应该一直输入它。如果你不想使用std,那就不要使用命名空间std;"
"在代码的开头。编译器

将一直要求std ::


如果你键入行,编译器不需要std :: any,

,因为你已经在命名空间std中了。



呃?我想你在谈论一个完全不同的问题。我不是在谈论使用而是在谈论使用在所有。


在文章< g7 ********** @ sagnix.uni-muenster.de> ;,上尉Trips

< us ******* @ uni-muenster.dewrote:


[Koenig查询]


嗯,它节省了很多打字,我认为这就是为什么它也适用于

功能。如果函数在命名空间std中有效,为什么

应该一直输入它。如果你不想使用std,那就不要使用命名空间std;"
"在代码的开头。编译器

将一直要求std ::。



这是他的观点;它不需要std :: here,即使没有

" sort"在main的范围内,并没有使用指令将命名空间std

纳入范围。只是因为table,函数

调用的一个参数是一个类型,其定义在命名空间std中,选择了

函数。


The so-called koenig lookup allows doing odd things like this:

#include <algorithm>
#include <string>

int main()
{
std::string table[10];
sort(table, table+10);
}

(Notice how the ''sort'' doesn''t have the std:: prefix, yet it''s
perfectly valid.)

I understand that the reason for the koenig lookup to exist is so that
you can write things like:

std::cout << 5;

This is actually equivalent to:

operator<<(std::cout, 5);

The koenig lookup allows the compiler to look for operator<< inside
the std namespace because one of the parameters is from that namespace.

With operators it makes sense. However, why perform the same lookup
for *all* functions? What''s the advantage? Couldn''t it be simply defined
that the koenig lookup is performed for operators but not for regular
functions?

解决方案

Juha Nieminen wrote:

The so-called koenig lookup allows doing odd things like this:

#include <algorithm>
#include <string>

int main()
{
std::string table[10];
sort(table, table+10);
}

(Notice how the ''sort'' doesn''t have the std:: prefix, yet it''s
perfectly valid.)

I understand that the reason for the koenig lookup to exist is so that
you can write things like:

std::cout << 5;

This is actually equivalent to:

operator<<(std::cout, 5);

The koenig lookup allows the compiler to look for operator<< inside
the std namespace because one of the parameters is from that namespace.

With operators it makes sense. However, why perform the same lookup
for *all* functions? What''s the advantage? Couldn''t it be simply defined
that the koenig lookup is performed for operators but not for regular
functions?

Well it saves a lot of typing, i think that''s why it''s working for
functions, too. If the function is valid in the namespace std, why
should you type it all the time. If you don''t want to use std just don''t
type "using namespace std;" at the beginning of your code. The compiler
will demand std:: all the time then.

If you type the line the compiler doesn''t need the std:: anymore,
because you are already in the namespace std.


Captain Trips wrote:

Well it saves a lot of typing, i think that''s why it''s working for
functions, too. If the function is valid in the namespace std, why
should you type it all the time. If you don''t want to use std just don''t
type "using namespace std;" at the beginning of your code. The compiler
will demand std:: all the time then.

If you type the line the compiler doesn''t need the std:: anymore,
because you are already in the namespace std.

Uh? I think you are talking about a completely different issue. I was
not talking about "using" at all.


In article <g7**********@sagnix.uni-muenster.de>, Captain Trips
<us*******@uni-muenster.dewrote:

[Koenig lookup]

Well it saves a lot of typing, i think that''s why it''s working for
functions, too. If the function is valid in the namespace std, why
should you type it all the time. If you don''t want to use std just don''t
type "using namespace std;" at the beginning of your code. The compiler
will demand std:: all the time then.

That was his point; it does NOT need std:: here, even though there is no
"sort" in the scope of main, and no using directive bringing namespace std
into scope. Simply because table, one of the arguments to the function
call, was of a type whose definition was in namespace std, the sort
function was chosen.


这篇关于为什么koenig查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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