Topic: requires required?
Author: "Joachim Faulhaber" <afojgo@googlemail.com>
Date: Tue, 3 Apr 2007 12:32:32 CST Raw View
I followed the recent developments of keyword 'where' for 'concepts'
and appreciate the decision to replace 'where' by 'requires'. Not only
is identifier 'where' now saved from being keyword bullied
(keyllied ;) in existing code. Also potential confusion with familiar
where-clause expression semantics from SQL or functional languages are
now avoided.
As I thought about the differences in the meanings between where and
requires I had the idea that keyword 'requires' could possibly NOT be
required.
Gurus please correct me if I got things wrong:
My understanding is, that a requires clause can be perceived as a
declaration.
It is equivalent to write
(1) template <typename T>
requires LessThanComparable<T>...
and
(2) template <LessThanComparable T> ...
In (1) we have an unspecified declaration of type T followed by the
requirement clause specifying T more precisely.
In (2) The parameter declaration states the specific property in the
declaration at once.
Isn't this like writing?
(3) template <T> LessThanComparable T; // not yet legal, but thinkable
So in the case of only one type parameter the requires-clause appeares
syntactically very much like a (postponed) declaration.
Semantically it *declares*: type T is a model of (conforms the
requirements of) concept LessThanComparable.
If "requires LessThanComparable<T>" has the character of a
declaration, why not use c++ declaration syntax and omit keyword
'requires'?
(1') template <typename T>
LessThanComparable T; ...
In the case of more than one type parameter the usual c++ declaration
syntax cannot be applied any more.
(4) template <typename T, typename U>
requires LessThanComparable<T>, EquivalenceRelated<T,U>; ...
still to me it appears that these have declarative character and could
be denoted like this:
(4') template <typename T, typename U>
LessThanComparable<T>; EquivalenceRelated<T,U>; ...
where Concept<T1,...,Tn>; is a generalized form of typedeclaration
that can stand for itself without keyname 'requires' required as many
other c++ declarations do. May be c++0x would be a little more lean
and elegant this way.
-- Joachim
---
[ 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: Tue, 3 Apr 2007 17:25:59 CST Raw View
On Apr 3, 2:32 pm, "Joachim Faulhaber" <afo...@googlemail.com> wrote:
> As I thought about the differences in the meanings between where and
> requires I had the idea that keyword 'requires' could possibly NOT be
> required.
Just to be clear: the change from "where" to "requires" is a purely
syntactic change. No semantics have changed at all in the concepts
proposal as a result of this. "where" became "requires" because
"where" is used as an identifier in far to much existing code.
> It is equivalent to write
> (1) template <typename T>
> requires LessThanComparable<T>...
> and
> (2) template <LessThanComparable T> ...
>
> In (1) we have an unspecified declaration of type T followed by the
> requirement clause specifying T more precisely.
> In (2) The parameter declaration states the specific property in the
> declaration at once.
>
> Isn't this like writing?
> (3) template <T> LessThanComparable T; // not yet legal, but thinkable
Well, you've skipped whatever comes after this second 'T'. Template
headers and requirements clauses aren't declarations by themselves;
they precede actual declarations:
template<typename T>
requires LessThanComparable<T>
const T& min(const T& x, const T& y) { return x < y? x : y; }
> (4') template <typename T, typename U>
> LessThanComparable<T>; EquivalenceRelated<T,U>; ...
>
> where Concept<T1,...,Tn>; is a generalized form of typedeclaration
> that can stand for itself without keyname 'requires' required as many
> other c++ declarations do. May be c++0x would be a little more lean
> and elegant this way.
The only (big) issue I have with this is that semicolons are meant to
separate statements and declarations in C++, but now we have them in
the middle of a template declaration. It seems a bit out-of-line with
the design of C++.
Also, there's one use of the "requires" clause that people tend to
forget when looking for new syntax. One can have a requires clause
start a new declaration of a member function inside a class template,
e.g.,
template<typename T>
class list {
public:
requires LessThanComparable<T> void sort(); // sort is only
available when LessThanComparable<T>
};
Cheers,
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 ]