Topic: argument-dependent name lookup


Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/09/17
Raw View
m-morita@trc.rwcp.or.jp (Masao Morita) writes:
> What about global?
> In case of addition the same signature fucnction into global.
> namespace A { int foo(int,int); struct AC { operator int() { return 1; } }; }
> namespace B { int foo(int,int); struct BC { operator int() { return 2; } }; }
> int foo(int,int); // #4
> void bar() { A::AC a; B::BC b; int c = foo(a,b); /* #3 */ }
> In my tuition, the global one(#4) would be selected, but it is not alike?
> Is the call still ambigous? (according to the standard.)

Yes, it's still ambiguous. When argument-dependent name lookup finds
candidate functions, they are equal in preference to functions found
by ordinary name lookup. Only class member functions come first.
See 3.4.2/2.

> I have one more question. What about a constructor call?
>
> namespace A { struct Foo { Foo(int,int); }; struct AC { oprator int (); } }; }
> namespace B { struct Foo { Foo(int,int); }; struct BC { oprator int (); } }; }
> void bar() { A::AC a; B::BC b; Foo(a,b); /* #3 */ }
>
> Argument dependant lookup is not applied for a constructor call.
> Is this right?

Remember that if Foo is a type, Foo(a,b) is *not* a function call,
so argument-dependent name lookup does not apply. And a constructor
for an object can't be defined in a different namespace than the
object itself. In your example, the compiler should complain that
Foo is undefined.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: m-morita@trc.rwcp.or.jp (Masao Morita)
Date: 1999/09/17
Raw View
hhymie@prolifics.com wrote :
>But I think you meant to ask about the following situation:
>
>namespace A { int foo(int,int); struct AC { operator int() { return 1; } }; }
>namespace B { int foo(int,int); struct BC { operator int() { return 2; } }; }
>void bar() { A::AC a; B::BC b; int c = foo(a,b); /* #3 */ }
Thanks for your considerate correcting. This is my intended one!

>Now call #3 has A and B as associated namespaces because those are the
>namespaces in which the types of the parameters to the call are defined.

>The call will still be illegal, now because it's ambiguous.
I see. Signature "int A::foo(int,int)" and "int B::foo(int,int)" were in
the same level at the overload resolution. "Argument depend lookup" has no
precedence for each arguments. I understand that clearly now.

What about global?
In case of addition the same signature fucnction into global.

namespace A { int foo(int,int); struct AC { operator int() { return 1; } }; }
namespace B { int foo(int,int); struct BC { operator int() { return 2; } }; }
int foo(int,int); // #4
void bar() { A::AC a; B::BC b; int c = foo(a,b); /* #3 */ }

In my tuition, the global one(#4) would be selected, but it is not alike?
Is the call still ambigous? (according to the standard.)

I have one more question. What about a constructor call?

namespace A { struct Foo { Foo(int,int); }; struct AC { oprator int (); } }; }
namespace B { struct Foo { Foo(int,int); }; struct BC { oprator int (); } }; }
void bar() { A::AC a; B::BC b; Foo(a,b); /* #3 */ }

Argument dependant lookup is not applied for a constructor call.
Is this right?

M. Morita
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: m-morita@trc.rwcp.or.jp (Masao Morita)
Date: 1999/09/15
Raw View
Hi,
   I have a question about argument-dependent name lookup.
Please consider the following.

namespace A {
    struct AC {
        int foo(int,int); // #1
        operator int () { return 1; }
    };
}

namespace B {
    struct BC {
        int foo(int,int); // #2
        operator int () { return 2; }
    };
};

void bar() {
   A::AC a;
   B::BC b;
   int c = foo(a,b); // #3
}

Is #3 a legal function call? according to the standard.
If it is legal which fuction, which one call(#1 or #2)?

If it is illegal, what is the reason?
Is the reason ambiguty(#1 and #2)?
or the other reason?

A complier that I use, only says "undifined foo".

Thanks in advance.

M. Morita
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/09/15
Raw View
m-morita@trc.rwcp.or.jp (Masao Morita) writes:
>    I have a question about argument-dependent name lookup.
> Please consider the following.
> namespace A { struct AC { int foo(int,int); operator int() { return 1; } }; }
> namespace B { struct BC { int foo(int,int); operator int() { return 2; } }; }
> void bar() { A::AC a; B::BC b; int c = foo(a,b); /* #3 */ }
>
> Is #3 a legal function call? according to the standard.

No, because a member function must be called through an object.
But I think you meant to ask about the following situation:

namespace A { int foo(int,int); struct AC { operator int() { return 1; } }; }
namespace B { int foo(int,int); struct BC { operator int() { return 2; } }; }
void bar() { A::AC a; B::BC b; int c = foo(a,b); /* #3 */ }

Now call #3 has A and B as associated namespaces because those are the
namespaces in which the types of the parameters to the call are defined.

The call will still be illegal, now because it's ambiguous.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/09/16
Raw View
On 15 Sep 99 13:07:09 GMT, Masao Morita <m-morita@trc.rwcp.or.jp> wrote:

>namespace A {
>    struct AC {
>        int foo(int,int); // #1
>        operator int () { return 1; }
>    };
>}
>
>namespace B {
>    struct BC {
>        int foo(int,int); // #2
>        operator int () { return 2; }
>    };
>};
>
>void bar() {
>   A::AC a;
>   B::BC b;
>   int c = foo(a,b); // #3
>}

In #3 you call the function 'foo'.

If foo is a operator, the compiler looks in four places
   ::A::AC // ie, as if you had written "a.foo(b)" -- foo a member
   ::A     // namespace where 'a' lives -- foo a non-member
   ::B     // namespace where 'b' lives -- foo a non-member
   ::      // namespace where call "foo(a,b)" lives -- foo a non-member
for the declaration of the function.

If foo is not an operator, the compiler looks in three places
   ::A     // namespace where 'a' lives -- foo a non-member
   ::B     // namespace where 'b' lives -- foo a non-member
   ::      // namespace where call "foo(a,b)" lives -- foo a non-member
for the declaration of the function.


If there is nothing like "#define foo operator+", then foo is not an
operator, and the compiler looks for a non-member function 'foo' in
three places.  There is no such function, hence the error.


If you had this:

   namespace A {
       int foo(int,int); // #1
       struct AC { operator int () { return 1; } };
   }

   namespace B {
       int foo(int,int); // #2
       struct BC { operator int () { return 2; } };
   };

   void bar() {
      A::AC a;
      B::BC b;
      int c = foo(a,b); // #3
   }

then the call "foo(a,b)" would be illegal on the grounds that
foo(int,int) is defined in two places -- A::foo B::foo.


BTW, I'd use a member function like "AC::asInt() const" rather than
the operator conversion "AC::operator int() const".

--
--------------
siemel b naran
--------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]