Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Why a member function will override an free function with the samename even if the arguments does not match? (http://www.velocityreviews.com/forums/t636209-why-a-member-function-will-override-an-free-function-with-the-samename-even-if-the-arguments-does-not-match.html)

Peng Yu 09-19-2008 09:22 PM

Why a member function will override an free function with the samename even if the arguments does not match?
 
Hi,

The following code doesn't work at the commented line. I'm wondering
why C++ grammar is defined this way. Shouldn't C++ be smarter enough
to figure out whether an member function or a free function shall be
used?

Thanks,
Peng

#include <iostream>

struct A {
A(int a) : _a(a) { }
int _a;
};


void doit(const A &a) {
std::cout << a._a << std::endl;
}

struct B {
public:
B(const A &a) : _a(a) { }
void doit() {
doit(_a);// the member function is conflicted with the free
function.
}
private:
A _a;
};


int main () {
B b((A(2)));
b.doit();
}

peter koch 09-19-2008 09:59 PM

Re: Why a member function will override an free function with thesame name even if the arguments does not match?
 
On 19 Sep., 23:22, Peng Yu <PengYu...@gmail.com> wrote:
> Hi,
>
> The following code doesn't work at the commented line. I'm wondering
> why C++ grammar is defined this way. Shouldn't C++ be smarter enough
> to figure out whether an member function or a free function shall be
> used?
>
> Thanks,
> Peng
>
> #include <iostream>
>
> struct A {
> * * A(int a) : _a(a) { }
> * * int _a;
>
> };
>
> void doit(const A &a) {
> * std::cout << a._a << std::endl;
>
> }
>
> struct B {
> * public:
> * * B(const A &a) : _a(a) { }
> * * void doit() {
> * * * doit(_a);// the member function is conflicted with the free
> function.
> * * }
> * private:
> * * A _a;
>
> };
>
> int main () {
> * B b((A(2)));
> * b.doit();


Because the standard says so. Actually, I also believe that this is a
good solution. The alternative would be to look all possible functions
up and choose the best candidate, but that easily becomes obscure in
some cases.
If you want to call a global function, all you have to do is to scope
the name as (here) ::doit.

/Peter

James Kanze 09-20-2008 08:20 AM

Re: Why a member function will override an free function with thesame name even if the arguments does not match?
 
On Sep 19, 11:59 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
> On 19 Sep., 23:22, Peng Yu <PengYu...@gmail.com> wrote:
> > The following code doesn't work at the commented line. I'm
> > wondering why C++ grammar is defined this way. Shouldn't C++
> > be smarter enough to figure out whether an member function
> > or a free function shall be used?


> > #include <iostream>


> > struct A {
> > A(int a) : _a(a) { }
> > int _a;
> > };


> > void doit(const A &a) {
> > std::cout << a._a << std::endl;
> > }


> > struct B {
> > public:
> > B(const A &a) : _a(a) { }
> > void doit() {
> > doit(_a);// the member function is conflicted with the free
> > function.
> > }
> > private:
> > A _a;
> > };


> > int main () {
> > B b((A(2)));
> > b.doit();


> Because the standard says so. Actually, I also believe that
> this is a good solution. The alternative would be to look all
> possible functions up and choose the best candidate, but that
> easily becomes obscure in some cases.


More than obscure, it's fragile. Someone adds an unrelated
function to a header file, and suddenly, your code unexplainedly
stops working. Presumably, if you have provided a member
function, that's the one you want to call. (I'm not even sure
that ADL kicks in if there is a member function. I rather hope
not.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Salt_Peter 09-20-2008 08:41 AM

Re: Why a member function will override an free function with thesame name even if the arguments does not match?
 
On Sep 19, 5:22 pm, Peng Yu <PengYu...@gmail.com> wrote:
> Hi,
>
> The following code doesn't work at the commented line. I'm wondering
> why C++ grammar is defined this way. Shouldn't C++ be smarter enough
> to figure out whether an member function or a free function shall be
> used?


Absolutely not, name lookup is strictly defined by the standard in
such a way that ambiguity and loose lookups are not allowed (it gets
quite complicated / involved too). The point here is that regardless
which compiler, architecture or platform you run the program in, no
loose interpretations of the standard causes side-effects or varied
results.
Keep it simple and secure. That *is* smarter.
Case in point, you have a program with 10K lines of code, once you
learn how to decypher name lookup diagnostics, correcting the issue is
childs play. With loose lookups: forget it. You would probably not
even be aware that in fact an error occurred.

>
> Thanks,
> Peng
>
> #include <iostream>
>
> struct A {
> A(int a) : _a(a) { }
> int _a;
>
> };
>
> void doit(const A &a) {
> std::cout << a._a << std::endl;
>
> }
>
> struct B {
> public:
> B(const A &a) : _a(a) { }
> void doit() {
> doit(_a);// the member function is conflicted with the free
> function.
> }
> private:
> A _a;
>
> };
>
> int main () {
> B b((A(2)));
> b.doit();
>
> }



peter koch 09-20-2008 02:00 PM

Re: Why a member function will override an free function with thesame name even if the arguments does not match?
 
On 20 Sep., 10:20, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 19, 11:59 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
>
>
>
>
>
> > On 19 Sep., 23:22, Peng Yu <PengYu...@gmail.com> wrote:
> > > The following code doesn't work at the commented line. I'm
> > > wondering why C++ grammar is defined this way. Shouldn't C++
> > > be smarter enough to figure out whether an member function
> > > or a free function shall be used?
> > > #include <iostream>
> > > struct A {
> > > * * A(int a) : _a(a) { }
> > > * * int _a;
> > > };
> > > void doit(const A &a) {
> > > * std::cout << a._a << std::endl;
> > > }
> > > struct B {
> > > * public:
> > > * * B(const A &a) : _a(a) { }
> > > * * void doit() {
> > > * * * doit(_a);// the member function is conflicted with the free
> > > function.
> > > * * }
> > > * private:
> > > * * A _a;
> > > };
> > > int main () {
> > > * B b((A(2)));
> > > * b.doit();

> > Because the standard says so. Actually, I also believe that
> > this is a good solution. The alternative would be to look all
> > possible functions up and choose the best candidate, but that
> > easily becomes obscure in some cases.

>
> More than obscure, it's fragile. *Someone adds an unrelated
> function to a header file, and suddenly, your code unexplainedly
> stops working. *Presumably, if you have provided a member
> function, that's the one you want to call. *


And if you don't, it is very easy to tell the compiler otherwise. This
is a very good example.

> (I'm not even sure
> that ADL kicks in if there is a member function. *I rather hope
> not.)


I believe not, but as I have recently changed pc, I don't have my
draft standard ready.

/Peter


All times are GMT. The time now is 06:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.