What would C++ look like without ADL?

S

Steven T. Hatton

I'll admit, so far in my C++ experience, I've left ADL to the realm
of 'magic happens'. I really don't know when I might be using it. Can
someone provide examples of how ADL changes the behavior of 'typical' code?
If it were not available, would that absence completely change the way we
use the STL or do other things?

If anybody is wondering, yes, I am curious about the ROI of having ADL in
the C++ language specification. It seems to significantly complicate the
language design, and can, at times introduce subtle anomalies which can be
hard to understand. Sometimes I see people doing fancy things with
typedefs to "prime" the ADL pump, and I wonder how much typing they are
actual avoiding, and how much cleaner the resulting code really is.

"Alles sollte so einfach wie möglich gemacht sein, aber nicht einfacher."
 
G

Greg

Steven said:
I'll admit, so far in my C++ experience, I've left ADL to the realm
of 'magic happens'. I really don't know when I might be using it. Can
someone provide examples of how ADL changes the behavior of 'typical' code?
If it were not available, would that absence completely change the way we
use the STL or do other things?

"Hello, world" with argument dependent lookup (ADL):

#include <iostream>

int main()
{
std::cout << "Hello, world!\n";
}

...and without:

#include <iostream>

int main()
{
std::cout.operator<<("Hello, world!\n");
}

Greg
 
P

peter koch

Greg skrev:
"Hello, world" with argument dependent lookup (ADL):

#include <iostream>

int main()
{
std::cout << "Hello, world!\n";
}

...and without:

#include <iostream>

int main()
{
std::cout.operator<<("Hello, world!\n");
}
And complex math with ADL:

int main()
{
std::complex<double> c1,c2,c3;
...
c1 = 2*c2 + c3;
}

And without
int main()
{
std::complex<double> c1,c2,c3;
...
c1 = std::eek:perator+(std::eek:perator*(2,c2),c3);
}

/Peter
 
S

Steven T. Hatton

Greg said:
"Hello, world" with argument dependent lookup (ADL):

#include <iostream>

int main()
{
std::cout << "Hello, world!\n";
}

...and without:

#include <iostream>

int main()
{
std::cout.operator<<("Hello, world!\n");
}

Greg

There are alternatives.
//bad
using namespace std;

//better but burdensome
using namespace std::eek:perator<<;//I believe that's the correct syntax

There could also be headers with such using declarations in them. For
example they could be part of <iostream>. The argument against doing
something like that is likely to be "we don't want to pollute the global
namespace". As a general rule, I am a strong believer in that principle.
OTOH, I'm not sure that ADL doesn't already introduce hidden pollutants.

I certainly take issue with Danny's conclusions, but he does raise some
interesting issues in this article (rant?):
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=286&rl=1
 
S

Salt_Peter

Steven said:
I'll admit, so far in my C++ experience, I've left ADL to the realm
of 'magic happens'. I really don't know when I might be using it. Can
someone provide examples of how ADL changes the behavior of 'typical' code?
If it were not available, would that absence completely change the way we
use the STL or do other things?

If anybody is wondering, yes, I am curious about the ROI of having ADL in
the C++ language specification. It seems to significantly complicate the
language design, and can, at times introduce subtle anomalies which can be
hard to understand. Sometimes I see people doing fancy things with
typedefs to "prime" the ADL pump, and I wonder how much typing they are
actual avoiding, and how much cleaner the resulting code really is.

"Alles sollte so einfach wie möglich gemacht sein, aber nicht einfacher.."
--

#include <iostream>
#include <ostream>
#include <string>

int main()
{
std::string s("a short string");
std::cout << s << std::endl;
// std::cout.operator<<(s); error, no match
std::cout.operator<<(s.c_str()); // prints pointer
}

/*
a short string
0x502028
*/
 
S

Steven T. Hatton

Salt_Peter said:
#include <iostream>
#include <ostream>
#include <string>

int main()
{
std::string s("a short string");
std::cout << s << std::endl;
// std::cout.operator<<(s); error, no match
std::cout.operator<<(s.c_str()); // prints pointer
}
I'm not advocating here, but this will work:

#include <iostream>
#include <string>

int main()
{
std::string s("a short string");
std::cout << s << std::endl;
operator<<(std::cout,s);
std::endl(std::cout);
std::cout.operator<<(s.c_str()); // prints pointer
std::endl(std::cout);
}
 
H

Howard

Steven T. Hatton said:
I'll admit, so far in my C++ experience, I've left ADL to the realm
of 'magic happens'. I really don't know when I might be using it. Can
someone provide examples of how ADL changes the behavior of 'typical'
code?
If it were not available, would that absence completely change the way we
use the STL or do other things?

If anybody is wondering, yes, I am curious about the ROI of having ADL in

ROI? ADL? You might consider that not everyone knows what those acronyms
mean.
the C++ language specification. It seems to significantly complicate the
language design, and can, at times introduce subtle anomalies which can be
hard to understand. Sometimes I see people doing fancy things with
typedefs to "prime" the ADL pump, and I wonder how much typing they are
actual avoiding, and how much cleaner the resulting code really is.

"Alles sollte so einfach wie mvglich gemacht sein, aber nicht einfacher."

And most of us don't speak German, either, assuming that's what that is.
 
F

F.J.K.

Howard schrieb:

And most of us don't speak German, either, assuming that's what that is.

A famous citation of Albert Einstein. "All should be done as easy as
possible, but not easier."
 

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,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top