Topic: Concepts and the "more specialized" relationship
Author: jgottman@carolina.rr.com (Joe Gottman)
Date: Sun, 13 May 2007 14:51:16 GMT Raw View
I just realized that there's a subtle difference between classes and
concepts that might affect the "more specialized" relationship that is
used for overload resolution. Classes use inheritance to model the "isa"
relationship.
class A {}
class B : public A {} // All B's are A's.
We can can do the same thing with concepts, although in this case it is
called refinement.
concept InputIterator<typename Iter> { }
concept ForwardIterator<typename Iter> : InputIterator<Iter> { }
//All ForwardIterators are InputIterators
However there's another, completely different way to make two concepts
model the "isa" relationship: concept_maps.
concept Range<typename R> {}
concept Container<typename C> {}
template <Container C> concept_map Range<C> { } //All Containers are
Ranges.
In this case, is the Container concept considered to be more specialized
than the Range concept for overload resolution? Can I define the
following overload set:
template <Range R> inline std::size_t size(const R &r);
template <Container C> inline std::size_t size(const C &c);
and have the second function called for all Containers and the first one
called for all Ranges that aren't Containers? If I can't do it
directly, will the following variation work?
template <Range R> inline std::size_t size(const R &r);
template <Range R> requires Container<R>
inline std::size_t size(const R &r);
Joe Gottman
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: Douglas Gregor <doug.gregor@gmail.com>
Date: Mon, 14 May 2007 18:46:00 CST Raw View
On May 13, 8:51 am, jgott...@carolina.rr.com (Joe Gottman) wrote:
> However there's another, completely different way to make two concepts
> model the "isa" relationship: concept_maps.
>
> concept Range<typename R> {}
> concept Container<typename C> {}
>
> template <Container C> concept_map Range<C> { } //All Containers are
> Ranges.
>
> In this case, is the Container concept considered to be more specialized
> than the Range concept for overload resolution?
Yes.
> Can I define the following overload set:
>
> template <Range R> inline std::size_t size(const R &r);
> template <Container C> inline std::size_t size(const C &c);
>
> and have the second function called for all Containers and the first one
> called for all Ranges that aren't Containers?
Yes.
> If I can't do it
> directly, will the following variation work?
>
> template <Range R> inline std::size_t size(const R &r);
> template <Range R> requires Container<R>
> inline std::size_t size(const R &r);
That'll work, too.
- Doug
---
[ 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.comeaucomputing.com/csc/faq.html ]