What the operator can be used without quantify with namespace?

P

PengYu.UT

I'm confused with the following small program. I looked at the
<complex> header file (gcc-3.3). operator* is defined in namespace std.
I'm wondering why the operator * can be used in the following program
even without quantify with namespace std.

Thanks,
Peng

#include <complex>

int main(int argc, char *argv[])
{
std::complex<double> a;
std::complex<double> b;
std::complex<double> c = a * b;
}
 
S

Sumit Rajan

I'm confused with the following small program. I looked at the
<complex> header file (gcc-3.3). operator* is defined in namespace std.
I'm wondering why the operator * can be used in the following program
even without quantify with namespace std.

Thanks,
Peng

#include <complex>

int main(int argc, char *argv[])
{
std::complex<double> a;
std::complex<double> b;
std::complex<double> c = a * b;
}

Look up Koenig Lookup (ADL):
http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,990,00.html
http://en.wikipedia.org/wiki/Koenig_Lookup

Regards,
Sumit.
 
N

Neelesh

I'm confused with the following small program. I looked at the
<complex> header file (gcc-3.3). operator* is defined in namespace std.
I'm wondering why the operator * can be used in the following program
even without quantify with namespace std.

Thanks,
Peng

#include <complex>

int main(int argc, char *argv[])
{
std::complex<double> a;
std::complex<double> b;
std::complex<double> c = a * b;
}


Argument Dependent Lookup (ADL).
A function name is looked up in the globally available namespaces and
the namespaces of its all arguments to find an exact matching
declaration.
Since a and b are of type std::complex<double>, the namespace std will
also be searched for finding exact declaration of *
 
E

Eric Pruneau

I'm not sure if I understand your question but here's what I think...

operator * is a function of the class complex...

just like push_back is a function of vector.

when you use push_back, you dont do:
vec.std::push_back(..) // do not try this at home

So why would you do use std:: before * ???

Think about it

Eric
 
E

Eric Pruneau

oupss forget about my last message...

I was thinking about *=



Eric Pruneau said:
I'm not sure if I understand your question but here's what I think...

operator * is a function of the class complex...

just like push_back is a function of vector.

when you use push_back, you dont do:
vec.std::push_back(..) // do not try this at home

So why would you do use std:: before * ???

Think about it

Eric


I'm confused with the following small program. I looked at the
<complex> header file (gcc-3.3). operator* is defined in namespace std.
I'm wondering why the operator * can be used in the following program
even without quantify with namespace std.

Thanks,
Peng

#include <complex>

int main(int argc, char *argv[])
{
std::complex<double> a;
std::complex<double> b;
std::complex<double> c = a * b;
}
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top