Remove abbreviated functions and template-introduction syntax from the
Concepts TS

Introduction

The abbreviated function and template introducer syntax features defined
in the Concepts TS [Concepts]
have proven to be controversial as evidenced by discussion within the
committee reflectors
[ExploringConcepts]
and the
P0587R0
[P0587R0]
and
P0464R2
[P0464R2]
paper submissions. This paper proposes removing these features from the
Concepts TS with the goal of increasing consensus on adopting the remaining
Concepts TS functionality into the current working paper.

Removal of support for abbreviated functions means that the following
example that is currently well-defined by the Concepts TS will no longer be
valid:

This paper does not propose removing support for declaring
functions and function templates with return types containing placeholders.
The following examples retain their current behavior under the Concepts TS:

template<typename, typename> class ct {};
ct<auto,auto> f1(); // Ok; declares a function with a return type// that requires the template arguments of ct// to be deduced from a return statement.
ct<C,C> f2(); // Ok; declares a function with a return type// that requires the template arguments of ct// to be deduced from a return statement, to be deduced// to the same type, and to satisfy C.

Removal of support for template introducers means that the following examples
that are currently well-defined by the Conepts TS will no longer be valid:

This paper does not propose removing support for declaring
parameters of generic lambdas with constrained-type-specifiers, nor
does it alter the current requirement that the same types be deduced for
equivalently constrained parameters. The following examples retain their
current behavior under the Concepts TS:

Motivation

The abbreviated function and template introducer syntax defined by the
Concepts TS is intended to reduce boilerplate, improve readability, and
ultimately, make generic programming more accessible as elaborated in
P0557R0
[P0557R0].
These are laudible goals and ones the author is in favor of continued pursuit.
However, the features currently defined in the Concepts TS raise concerns
regarding mutation of code undergoing maintenance and difficulties programmers
may face in navigating the differences in requirements and behaviors exhibited
by functions and function templates that are made more subtle by the current
design.

Consider the following function declaration. Does it declare a function or
a function template?

void f(X x) {}

Prior to the Concepts TS, this unambiguously declares a non-template function.
However, if X names a concept, then f is an abbreviated
function equivalent to the following function template:

template<X T> void f(T x) {}

This is exactly the intent as described in
P0557R0
[P0557R0]; one may now write generic functions as familiar to any
C programmer without the boilerplate currently required to author a
function template!

The problem is, this simplicity is an illusion that dissolves under further
inspection as illustrated in the following examples.

void f1(X&& x) {}

If X names a concept, then parameter x is a forwarding
reference; otherwise, it is an rvalue reference and f1 may only be
called with an rvalue argument.

f2 maintains a count of either every invocation of f2, or
every invocation of an instantiated specialization of f2 depending
on whether X names a concept or a type.

void f3(X x) {}

f3 may be moved from a primary source file to a header if X
names a concept, but doing so risks introducing multiple definition errors if
X names a type.

void f4(int i) {}

If f4 were modified to:

void f4(int i, X x) {}

then its definition will change from a non-template function to a template
function if X names a concept, but will remain a function if X
names a type.

void f5(Ziggle z, Piddle p) {
Ziggle::type x1; // Ok if Ziggle is a type, ill-formed if it's a concept.
decltype(p)::type x2; // Ok if Piddle is a type, needs typename if it is a concept.
typename decltype(z)::type x3; // Ok if Ziggle is a concept, ill-formed if it is a type.
}

f5 illustrates the increased subtlety in determining when
typename disambiguation is required. The presence of a
template-parameter-list enables such determination without having
to be familiar with the particular identifiers:

Abbreviated functions have the behavior that placeholders specified within
the declared return type of the function that match a
constrained-type-specifier used in a parameter type are not deduced,
but are rather replaced by the deduced type for the matching parameter(s).
However, this behavior doesn't hold if the same return type is specified as a
trailing-return-type: [ Note: It is not clear that this is
the intent of the Concepts TS, but this is the behavior exhibited by gcc.
— end note ]

A semantic difference based on whether the return type of a function is
specified in the declared return type as opposed to a
trailing-return-type is novel and introduces the possibility of subtle
defects being introduced due to a change a programmer expects to be purely
stylistic. Similarly, this behavior introduces the possibility that a change
to the declaration of a function parameter may alter the meaning of the return
type: [ Note: gcc 6.2-7.1 rejects both of the following cases as it
(presumably erroneously) replaces C in the return type with the
invented template parameter for the first parameter regardless of whether the
first parameter is declared with a matching constrained-type-specifier.
— end note ]

The template introduction syntax does not suffer concerns like those above.
The primary concern with it is aesthetic; that it lacks cohesiveness with the
rest of the language as described in
P0587R0
[P0587R0]. The choice to remove it as proposed in this paper is
based on a perception that an alternate syntax to replace abbreviated function
syntax might also encompass the goals of template introductions.

Few of the concerns raised above are applicable to every template
declaration and it has not been claimed that either of the terse syntaxes
is the best fit for all template declarations. Is it not therefore
reasonable for the use of these syntaxes to be a matter of code style? Do
tools such as IDEs that provide semantic markup not help to counteract these
concerns?

The examples above suggest that guidelines for the usage of abbreviated
function syntax can be crafted. A simple guideline might look like:

Write abbreviated functions that have return types containing placeholders
using a trailing-return-type.

Whether the concerns presented in this paper rise above a level adequately
addressed by guidelines and coding standards is a matter of judgement and
no arguments are presented here to attempt to establish a litmus test.

IDEs and other tools that provide semantic markup are certainly helpful.
However, not all programmers use IDEs and most of us use tools that lack
semantic awareness; particularly for tools used for code review, diffing,
and merging. Relying on semantic awareness to help elucidate code is likely
to result in disappointment in various parts of our collective work flows
for the forseeable future.

Wording

Hide deleted text

These changes are relative to
a draft of the Concepts TS working paper being prepared for the 2017 Toronto
pre-meeting mailing
[ConceptsWP]

Change the added paragraph 6 inserted in 8.1.4.2 [expr.prim.id.qual]:

In a nested-name-specifier of the form auto:: or C::,
where C is a constrained-type-name, that
nested-name-specifier designates a placeholder that will be replaced
later according to the rules for placeholder deduction in 10.1.7.4. If a
placeholder designated by a constrained-type-specifier is not a
placeholder type, the program is ill-formed. [ Note: A
constrained-type-specifier can designate a placeholder for a non-type
or template (10.1.7.4.2). — end note ] The replacement type
deduced for a placeholder shall be a class or enumeration type. [
Example:

In the declaration of f, the placeholder appears in a non-deduced
context (17.8.2.5). It may be replaced later through the explicit specification
of template arguments.
— end example ]

Change in 8.1.5.1 [expr.prim.lambda.closure]:

Modify paragraph 3 so that the meaning of a generic lambda is defined in terms
of its abbreviated member function template call operator.

[…] The closure type for a generic lambda has a public inline function
call operator member template that is an abbreviateda
function template whose parameters and return type are derived from the
lambda-expression's parameter-declaration-clause and
trailing-return-type according to the rules in
(11.3.5)(10.1.7.4.1).

Add the following example after those in paragraph 3 in the C++ Standard.

The compound-requirement in C5 introduces two constraints:
an expression constraint for f(x), and a deduction constraint
requiring that overload resolution succeeds for the call g(f(x))
where g is the following invented abbreviated function
template.

template<C T> void g(const CT&);

— end example ]

Change within the replacement text in 10.1.7.4 [dcl.spec.auto] paragraph 1:

Replace paragraph 1 with the text below.

[…] The type-specifiers auto and
decltype(auto) and constrained-type-specifiers designate a
placeholder (type, non-type, or template) that will be replaced later, either
through deduction or an explicit specification. The auto and
decltype(auto)type-specifiers designate placeholder types;
a constrained-type-specifier can also designate placeholders for
values and templates. [ Note: The deduction of placeholders is done
through the invention of template parameters as described in 10.1.7.4.1
and 11.3.5. — end note ] Placeholders are also used
to signify that a lambda is a generic lambda (8.1.5), that a function
declaration is an abbreviated function template (11.3.5), or that a
trailing-return-type in a compound-requirement (8.1.7.3)
introduces an argument deduction constraint (17.10.1.6). The autotype-specifier is also used to introduce a function
type having a trailing-return-type or to introduce a structured
binding declaration 11.5. [ Note: A nested-name-specifier
can also include placeholders (8.1). Replacements for those placeholders
are determined according to the rules in this section.
— end note ]

Remove the allowance for placeholders appearing in the parameter type of
a function declaration in the added text in 10.1.7.4 [dcl.spec.auto]
paragraph 3:

[…]
— end example ] Similarly, if a placeholder appears in
a parameter type of a function declaration, the function declaration declares
an abbreviated function template (11.3.5). [ Example:

void f(const auto&, int); // OK: an abbreviated function template

— end example ]

Remove the added paragraph 4 inserted in 10.1.7.4 [dcl.spec.auto]:

Add the following after paragraph 3 to describe when
constrained-type-specifiers in the return type refer to template
parameters.

A constrained-type-specifierC1 within the declared
return type of an abbreviated function template declaration does not designate
a placeholder if its introduced constraint-expression (10.1.7.4.2)
is determined to be equivalent, using the rules in 17.6.5.1 for comparing
expressions, to the introduced constraint-expression for a
constrained-type-specifierC2 in the
parameter-declaration-clause of that function declaration. Instead,
C1 is replaced by the template parameter invented for C2
(11.3.5). [ Example:

In the declaration f1, the constraint-expression
introduced by the constrained-type-specifiers in the
parameter-declaration-clause and return type are equivalent;
they would both introduce the expression C<T>, for some invented
template parameter T. In f2, the use of C in the
return type would introduce the constraint-expression(C<T> &l&l ...), which is distinct from the
constraint-expressionC<T> introduced by the invented
constrained-parameter (17.1) for the
constrained-type-specifier in the
parameter-declaration-clause according to the rules in 11.3.5.
— end example

Renumber the added paragraphs 5 and 6 added to 10.1.7.4 [dcl.spec.auto] to
4 and 5 respectively.

Replace the changes to 10.1.7.4.1 [dcl.spec.auto.deduct] paragraph 2
with the following:

A type T containing a placeholder typeplaceholders,
and a corresponding initializer e, are determined as follows:

(2.1) — for a non-discarded return statement that occurs in a
function declared with a return type that contains a placeholder typeplaceholders, T is the declared return type and e
is the operand of the return statement. If the return
statement has no operand, then e is void();

(2.2) — for a variable declared with a type that contains
a placeholder typeplaceholders, T is the
declared type of the variable and e is the initializer. If the
initialization is direct-list-initialization, the initializer shall
be a braced-init-list containing only a single
assignment-expression and e is the
assignment-expression;

(2.3) — for a non-type template parameter declared with a type that
contains a placeholder typeplaceholders, T
is the declared type of the non-type template parameter and e
is the corresponding template argument.;

(2.4) — for a parameter of a generic lambda-expression
declared with a type that contains placeholders, T is the declared
type of the parameter and e is the corresponding argument in an
invocation of the generic lambda's function call operator template;

(2.5) — for an argument deduction constraint (17.10.1.6),
T is the trailing-return-type, and e is the
expression of the corresponding compound-requirement.

In the case of a return statement with no operand or with an operand
of type void, T shall be either decltype(auto)or,cvauto, or a
constrained-type-specifier.

Correct the changes to 10.1.7.4.1 [dcl.spec.auto.deduct] paragraph 3 to
reference paragraph 4 from the C++ standard.

Replace the changes to 10.1.7.4.1 [dcl.spec.auto.deduct] paragraph 4
with the following:

If the placeholder isplaceholders include the
autotype-specifieror a
constrained-type-specifier, the deduced type T'
replacing T is determined using the rules for template argument
deduction. If T corresponds to the type of a parameter of a
generic lambda-expression, then template argument deduction is
performed for all parameter types containing placeholders together.Obtain P from T by replacing the
occurrences of auto with either a new invented type template
parameter U or, if the initialization is
copy-list-initialization, with
std::initializer_list<U>.Obtain a type
P from T by replacing each placeholder as follows:

(4.1) — if the initialization is a copy-list-initialization
and a placeholder is a decl-specifier of the
decl-specifier-seq of the variable declaration, replace that
occurrence of the placeholder with std::initializer_list<U>
where U is an invented type template parameter;

(4.2) — otherwise, if the placeholder is designated by the
autotype-specifier, replace the occurrence with a new
invented type template-parameter or, if T corresponds to
a function parameter pack, with a new invented type
template parameter pack;

(4.3) — otherwise, the placeholder is designated by a
constrained-type-specifier. If a placeholder in T or,
for parameters of generic lambda-expressions, any preceding
parameter, has already been replaced by an invented
constrained-parameter (17.1) with constraint-expressions
(17.1) equivalent according to the rules for comparing expressions
in 17.6.5.1 to a constrained-parameter whose
qualified-concept-name is that of the
constrained-type-specifier, then replace the occurrence with the
existing invented constrained-parameter. Otherwise, replace the
occurrence with a new invented constrained-parameter whose
qualified-concept-name is that of the
constrained-type-specifier.

Deduce a value for Ueach invented template
parameter using the rules of template argument deduction from a
function call (17.8.2.1), where P is a function template parameter
type and the corresponding argument is e. [ Note:
multiple function template parameters will be present when deducing types
for generic lambda-expressions with multiple parameters with
types containing placeholders. — end note ] If the
deduction fails, the declaration is ill-formed. If any placeholders
in the declared type were introduced by a constrained-type-specifier,
then define C to be a constraint-expression as follows:

(4.4) — if there is a single constrained-type-specifier,
then C is the constraint-expression introduced by the
invented template constrained-parameter (17.1) corresponding to that
constrained-type-specifier;

(4.5) — otherwise, C is the logical-and-expression
(8.14) whose operands are the constraint-expressions introduced by the
invented template constrained-parameters corresponding to each
constrained-type-specifier, in order of appearance.

If the normalized constraint for C (17.10.2) is not satisfied
by the deduced values, the declaration is ill-formed. Otherwise,
T' is obtained by substituting the deduced Uvalues for the invented type template parameters into P.
[ Example:

The constrained-type-specifiers C and D<0>
correspond to distinct invented template parameters in the declaration of the
function call operator template of the closure type.

auto gl2 = [](C a, C b) {};

The types of a and b are the same invented template type
parameter.

auto gl3 = [](C& a, C* b) {};

The type of a is a reference to an invented template type parameter
T, and the type of b is a pointer to T.

auto gl4 = [](N::C a, C b) {};
auto gl5 = [](D<0> a, D<1> b) {};

In both lambda-expressions, the parameters a and b
have different invented template type parameters.

auto gl6 = [](E a, E<> b, E<0> c) {};

The types of a, b, and c are the same because the
constrained-type-specifiers E, E<>, and
E<0> all associate the constraint-expressionE<T, 0>, where T is an invented template type parameter.

auto gl7 = [](C head, C... tail) {};

The types of head and tail are different. Their respective
introduced constraint-expressions are C<T> and
(C<U> && ...), where T is the template
parameter invented for head and U is the template parameter
invented for tail (17.1).
— end example ]

An identifier is a concept-name if it refers to a set of concept
definitions (10.1.8). [ Note: The set of concepts has multiple members
only when referring to a set of overloaded function concepts. There is at most
one member of this set when a concept-name refers to a variable
concept. — end note ] [ Example:

[…] The rules for inventing template parameters corresponding to
placeholders in the parameter-declaration-clause of a
lambda-expression (8.1.2) or function declaration (11.3.5)
are described in 11.3.510.1.7.4.1 […]

A template-declaration is written in terms of its template parameters.
These parameters are declared explicitly in a template-parameter-list
(14.1), or they are introduced by a
template-introduction (14.2). The optional
requires-clause following a template-parameter-list
allows the specification of constraints (14.10.2) on template arguments (14.4).

Remove the added 17.2 [temp.intro]:

Add this section after 17.1.

1 A template-introduction […]

2 The concept designated by […]

3 A concept referred to […]

4 An introduced template parameter […]

5 A template-introduction introduces […]

6 A template declared by a template-introduction […]

Change in 17.10.1.6 [temp.constr.deduct] paragraph 2:

To determine if an argument deduction constraint is satisfied, invent
an abbreviateda function template f with
one parameter whose type is T(11.3.5)(10.1.7.4.1)
The constraint is satisfied if the resolution of the function call
f(E) succeeds (16.3). [ Note: Overload resolution
succeeds when values are deduced for all invented template parameters
in f that correspond to the placeholders in T, and the
constraints associated by any constrained-type-specifiers are
satisfied. — end note ] [ Example:

The invented abbreviated function template f for the
compound-requirement in C2 is:

template<C1 T1, typename T2> void f(Pair<C1T1&, autoT2>);

In the call g((int*)nullptr), the constraints are not satisfied
because no values can be deduced for the placeholders C1 and
auto from the expression *t when t has type
“pointer-to-int”. — end example ]

Change in 17.10.2 [temp.constr.decl] paragraph 2:

Constraints can also be associated with a declaration through the use of
template-introductions, constrained-parameters
in a template-parameter-list, and
constrained-type-specifiers in the parameter-type-list
of a function template. Each of these forms introduces additional
constraint-expressions that are used to constrain the declaration.
A template’s associated constraints are defined as a single
constraint-expression derived from the introduced
constraint-expressions using the following rules.

(2.1) — If there are no introduced constraint-expressions,
the declaration is unconstrained.

(2.2) — If there is a single introduced constraint-expression,
that is the associated constraint.

(2.3) — Otherwise, the associated constraints are formed as a logical AND
expression (8.14) whose operands are in the following order:

(2.3.1) — the constraint-expression introduced by a
template-introduction (17.2), and

(2.3.21) — the constraint-expression
introduced by each constrained-parameter (17.1) in the declaration's
template-parameter-list, in order of appearance, and

(2.3.32) — the constraint-expression
introduced by a requires-clause following a
template-parameter-list (Clause 17), and

(2.3.4) — the constraint-expression introduced by each
constrained-type-specifier (10.1.7.4.2) in the type of a
parameter-declaration in a function declaration (11.3.5), in order of
appearance, and

(2.3.53) — the constraint-expression
of a trailing requires-clause (Clause 11) of a function declaration
(11.3.5).

The formation of the associated constraints for a template declaration
establishes the order in which the normalized constraints (defined below) will
be compared for equivalence (to determine when one template redeclares
another), and the order in which constraints are instantiated when checking for
satisfaction (17.10.1). The constraint-expressions introduced by
constrained-type-specifiers in a variable type or in the declared
return type of a function are not included in the associated constraints of a
template declaration. [ Note: These constraints are checked during
the instantiation of the declaration. — end note ] A program
containing two declarations whose associated constraints are functionally
equivalent but not equivalent (14.6.6.1) is ill-formed, no diagnostic required.
[ Example:

The associated constraints of the first declaration are
C1<T> && C2<T>, and those of the second are
C2<T> && C1<T>. — end example ]

Change in 17.10.4 [temp.constr.resolve] paragraph 1:

Concept resolution is the process of selecting a concept from a set of
concept definitions referred to by a qualified-concept-name, or from a
set of declarations including one or more concept definitions referred to by a
simple-template-id or a qualified-id whose
unqualified-id is a simple-template-id. Concept resolution
is performed when such a name appears

(1.1) — as a constrained-type-specifier (10.1.7.4.2),

(1.2) — in a constrained-parameter (17.1),

(1.3) — in a template-introduction (17.2), or

(1.43) — within a constraint-expression
(17.10.2).

Within such a name, let C be the concept-name or
template-name that refers to the set of concept definitions.

Change in 17.10.4 [temp.constr.resolve] paragraph 3:

The method for determining the concept argument list depends on the context in
which C appears.

(3.1) — If C is part of a constrained-type-specifier
or constrained-parameter, then

(3.1.1) — if C is a constrained-type-name, the concept
argument list is comprised of a single wildcard, or

(3.1.2) — if C is the concept-name of a
partial-concept-id, the concept argument list is comprised of a single
wildcard followed by the template-arguments of that
partial-concept-id.

(3.2) — If C is the concept-name in a
template-introduction. the concept argument list is a sequence of
wildcards of the same length as the introduction-list of the
template-introduction.

(3.32) — If C appears as a
template-name of a simple-template-id, the concept argument
list is the sequence of template-arguments of that
simple-template-id.

Change the example in in 17.10.4 [temp.constr.resolve] paragraph 4 to
replace the use of abbreviated functions and template introducers with
function template declarations:

[…]
If any concept arguments do not match a corresponding template parameter, the
concept CC is not a viable selection. The concept selected by concept
resolution shall be the single viable selection in the set of concepts referred
by C. [ Example: