J
Juha Nieminen
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?
#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?