Topic: Koenig lookup and classes/constructors


Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: Mon, 18 Jun 2001 07:20:12 GMT
Raw View
celtschk@web.de ("Christopher Eltschka") writes:

> Are classes/"constructor calls" are looked up via Koenig lookup?

No. 3.4.2 specifies that only postfix expressions in function calls,
as explained in 5.2.2, are subject to argument-dependent lookup,
i.e. the result of the lookup must be an expression, or a function
name. If your code was well-formed, you'd have an explicit type
conversion as explained in 5.2.3, where you'd need a
simple-type-specifier before the parentheses. Type specifiers are
never found by Koenig lookup.

> Therefore it _does_ find X::B, but doesn't consider the constructor a
> match. Changing B(A) to B(A&) doesn't change this. Is this correct
> behaviour?

I think so (not surprisingly :-) Interestingly enough, Sun CC 5.2 reports

"a.cc", line 16: Error: Unexpected type name "X::B" encountered.

so it also finds X::B ...

Regards,
Martin

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: celtschk@web.de ("Christopher Eltschka")
Date: Sat, 16 Jun 2001 01:22:40 GMT
Raw View
Are classes/"constructor calls" are looked up via Koenig lookup?

That is, should the following code compile?

namespace X
{
  class A {};

  class B
  {
    B(A) {}
  };
}

int main()
{
  X::A a;
  B(a); // creates temporary object of type X::B, or error?
}

Since there's no ::B, and from the syntax it might be a function call
(in some sense it _is_, namely a constructor call), the compiler will
inevitably look into namespace X during Koenig lookup. Now inside X,
it should find class B. Or does it only specifically look for "real"
functions?

BTW, g++ 2.95.2 gives a very interesting error message:

test.cc: In function `int main()':
test.cc:14: no match for call to `(X::B) (X::A &)'

Therefore it _does_ find X::B, but doesn't consider the constructor a
match. Changing B(A) to B(A&) doesn't change this. Is this correct
behaviour?

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]