Topic: Koenig lookup and members
Author: msherman@magma.ca (Marc Sherman)
Date: 1998/03/16 Raw View
In article <p6q1zw6lkyu.fsf@pandora.fb-inf2>,
Martin v. Loewis <loewis@informatik.hu-berlin.de> wrote:
>I have difficulties understanding [basic.lookup.koenig]. It defines
>the set of associated classes and namespaces, and then says that the
>set of declarations found is the union of those found using ordinary
>lookup and the set found in the namespaces and classes associated with
>the argument types.
I have a related question. Does koenig lookup extend to names from
one namespace brought into another with using declarations? IE: is
the following code legal (and does it do what I want)?
namespace enum_seq_ops
{template<class T> T& operator++(T& t){t = static_cast<T>(t+1); return t;}}
namespace MyEnum
{ enum MyEnum_t { a, b, c, d, e, end }; using enum_seq_ops; }
using MyEnum::MyEnum_t;
int main()
{
for(MyEnum_t i = MyEnum::a; i < MyEnum::end; i++);
}
- Marc
---
[ 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: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/03/17 Raw View
Marc Sherman<msherman@magma.ca> wrote:
>I have a related question. Does koenig lookup extend to names from
>one namespace brought into another with using declarations? IE: is
>the following code legal (and does it do what I want)?
>
>namespace enum_seq_ops
>{template<class T> T& operator++(T& t){t = static_cast<T>(t+1); return t;}}
>
>namespace MyEnum
>{ enum MyEnum_t { a, b, c, d, e, end }; using enum_seq_ops; }
>
>int main()
>{
> using MyEnum::MyEnum_t;
> for(MyEnum_t i = MyEnum::a; i < MyEnum::end; i++);
>}
This is not valid code. If you replace "using enum_seq_ops" above
with "using enum_seq_ops::operator++", then it works. If you
replace it with "using namespace enum_seq_ops", then it doesn't.
(Note that not all compilers that claim to support namespaces support
Koenig lookup, and that of those that claim to support it, if only for
operators, most don't support it correctly. In particular, the valid
usage described above is something that doesn't work in most, yet.)
It's worth noting that here, as is usually the case, namespace-declarations
give more intuitively useful results than a namespace-directive, despite
being a bit more verbose.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
---
[ 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: loewis@cs.tu-berlin.de (Martin v.Loewis)
Date: 1998/03/17 Raw View
In article <6ek30v$ahr$1@shell7.ba.best.com>,
Nathan Myers <ncm@nospam.cantrip.org> wrote:
>>namespace enum_seq_ops
>>{template<class T> T& operator++(T& t){t = static_cast<T>(t+1); return t;}}
>>
>>namespace MyEnum
>>{ enum MyEnum_t { a, b, c, d, e, end }; using enum_seq_ops; }
>>
>>int main()
>>{
>> using MyEnum::MyEnum_t;
>> for(MyEnum_t i = MyEnum::a; i < MyEnum::end; i++);
>>}
>
>This is not valid code. If you replace "using enum_seq_ops" above
>with "using enum_seq_ops::operator++", then it works. If you
>replace it with "using namespace enum_seq_ops", then it doesn't.
This analysis is wrong; either correction would work. For 'i++',
the compiler searches for the operator++, using Koenig lookup. Since
i's associated namespace is MyEnum, it searches for
MyEnum::operator++ ([basic.lookup.koenig]). Now, this is a plain
qualified namespace lookup as described in [basic.namespace.qual]. Since
there are no definitions of operator++ in namespace MyEnum, it considers
all using-directives in this namespace, and searches for operator++
in namespace enum_seq_ops, where it finds the template.
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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: loewis@informatik.hu-berlin.de (Martin v. Loewis)
Date: 1998/03/15 Raw View
I have difficulties understanding [basic.lookup.koenig]. It defines
the set of associated classes and namespaces, and then says that the
set of declarations found is the union of those found using ordinary
lookup and the set found in the namespaces and classes associated with
the argument types.
So what does it find in the associated classes? E.g. is
struct X{
static int f(X&);
};
X x;
int y(f(x));
correct? X is an associated class, so the class member should be
found. OTOH, the first paragraph mentions that only namespace members
and friends are found.
[over.match.oper] mentions that operator lookup proceeds like Koenig
lookup, only that member functions are ignored. Why do they need to
point this out, since Koenig lookup would not find class members in
the first place?
Any clarification appreciated,
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://reality.sgi.com/austern_mti/std-c++/faq.html ]