Hello All!
> On Sat, 7 May 2016 17:29:59 -0700 (PDT)
> "K. Frank" <...> wrote:
> > Hello Group!
> >
> > I (often) think of template classes as providing
> > compile-time "polymorphism." Is it fair to think
> > of concepts as interfaces for templates?
> >
> > If so, is it highly precise to think of concepts
> > as interfaces for templates, or would it be more
> > just a useful analogy, but "don't push it to far?"
>
Öö Tiib:
> IOW ordinary class type or even fundamental type may fit with
a concept.
Yes, good point. A class need not know or care about
a concept it satisfies. The concept could well be
defined after the class. (See below.)
Marcel Mueller:
> Well, at the end the question is, what you summarize
> with the term 'interface'.
By "interface" I mean pretty much what java interfaces
are, or what you get in c++ when you derive from a
pure abstract base class:
struct InterfaceA {
virtual void methodA1() = 0;
virtual void methodA2() = 0;
virtual void methodA3() = 0;
};
struct ClassImpA : InterfaceA {
void methodA1() {}
void methodA2() {}
void methodA3() {}
};
On Sunday, May 8, 2016 at 11:41:39 AM UTC-4, Chris Vine wrote:
> Concepts are type classes. They describe a subset of the set of all
> types, namely those that meet the concept requirements. If you find
> unnecessary OO an abstraction which you particularly like (I don't, I
> find it often adds obscurity) then I guess interfaces is one way of
> thinking about them, as a subset of the set of all inheritable
> implementation specifications.
>
> However at the end of the day they are just a named set of type
> constraints. So why not think of them as they are?
Well, yes, this is certainly true. But my question is
driven by why you would want to use a concept.
I guess from the perspective of using a function argument,
concepts are like interfaces:
template <typename T "satisfies ConceptA"> void f (T& someA) {
// do stuff that relies on ConceptA
// true, ConceptA is a named set of type constraints,
// but why would I care to name it? Because I care
// that for the purposes of this function someA "is a"
// ConceptA.
}
vs.
void f (InterfaceA& someA) {
someA.methodA3();
someA.methodA1();
}
But from the perspective of defining a class, concepts
and interfaces are different:
struct ClassImpA : InterfaceA { ... };
ClassImpA had better implement InterfaceA, and the
compiler will check that it does. Note, in particular,
that for a ClassImpA to be passed to f (InterfaceA&),
InterfaceA has to be defined in the translation unit
of ClassImpA.
But a class that satisfies a concept need not know
about the concept (and the concept may not even have
been defined yet), and, as Öö reminds us, even fundamental
types can satisfy concepts.
struct SomeClassThatHappensToSatisfyConceptA {
// or maybe it doesn't ...
};
Speaking of which, are there any concept proposals
that permit or encourage classes to formally specify
which concepts they intend to satisfy (and will be
verified to satisfy)?
Thanks for everyone's thoughts on this.
K. Frank