Topic: Concept inheritance vs. 'requires
Author: daniel.kruegler@googlemail.com
Date: Mon, 11 May 2009 13:23:50 CST Raw View
On 11 Mai, 08:47, "Boris Schaeling" <bo...@highscore.de> wrote:
> N2773 contains this example on page 57:
>
> concept C<typename T> { }
> concept D<typename T> {
> requires C<T>;
>
> }
>
> To make sure I understand concept inheritance and 'requires' right: Can
> this example be rewritten as
>
> concept C<typename T> { }
> concept D<typename T> : C<T> { }
>
> ? Is this the very same?
The latter is *not* exactly equivalent to the former.
The difference becomes observable, if you try to
define a concept map for D *without* having defined
a previous concept map for C like this:
concept C<typename T> { }
concept D<typename T> {
requires C<T>;
}
concept_map D<int>{}
This is ill-formed, because no concept map
for C yet exists and you need to provide one
before the concept map definition of D "has
been seen" by the compiler.
Let's use a refinement relation instead:
concept C<typename T> { }
concept D<typename T> : C<T> { }
concept_map D<int>{}
This is *well-formed* because [concept.refine.maps]
ensures that a concept map for C<int> is implicitly
created (Inside the concept map of D all requirements
are satisfied). The generation of this implicit concept
map is only produced, if no concept map for C has
been found and if D satisfies all requirements of
it's own and it's refinement bases.
In most other use-cases this difference does not
manifest.
E.g. requirement-implication ensures that even in
the non-refinement case a constrained template for
C will accept any archetype satisfying D, see
[temp.req.impl]/3:
"For every concept requirement in a template s
requirements (including implied requirements),
requirements for the refinements and associated
requirements of the concept named by the
concept instance (14.10.3, 14.10.1.3) are implied."
Here is an example where this becomes clearer:
template<C T>
void foo() {}
template<D T>
void bar() { foo<T>(); }
The C-foo can be called by bar because the C<T>
requirement is implied by the D requirement.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: SG <s.gesemann@gmail.com>
Date: Mon, 11 May 2009 13:23:05 CST Raw View
On 11 Mai, 08:47, "Boris Schaeling" <bo...@highscore.de> wrote:
> N2773 contains this example on page 57:
>
> concept C<typename T> { }
> concept D<typename T> {
> requires C<T>;
> }
>
> To make sure I understand concept inheritance and 'requires' right: Can
> this example be rewritten as
The correct word here is "refinement", not "inheritance".
> concept C<typename T> { }
> concept D<typename T> : C<T> { }
>
> ? Is this the very same?
Almost. Since D is a refinement of C a concept_map for D<T> will make
the type T automatically a model of C as well. Also, I would guess
that refinement plays an important role in concept-based overloading.
Consider this:
struct S {};
concept_map D<S> {}
concept_map C<S> {} // <-- redundant in case D refines C
template<C T> void foo(T t); // #1
template<D T> void foo(T t); // #2
void bar() {
foo(S()); // ambiguous or #2 ?
}
In case D is a refinement of C the call resolves to #2. Otherwise,
this call might be ambiguous (not sure, you have to check that).
Cheers!
SG
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Boris Schaeling" <boris@highscore.de>
Date: Mon, 11 May 2009 00:47:48 CST Raw View
N2773 contains this example on page 57:
concept C<typename T> { }
concept D<typename T> {
requires C<T>;
}
To make sure I understand concept inheritance and 'requires' right: Can
this example be rewritten as
concept C<typename T> { }
concept D<typename T> : C<T> { }
? Is this the very same?
Boris
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]