Document number: P1141R0

Ville Voutilainen
Thomas Köppe
Andrew Sutton
Herb Sutter
Gabriel Dos Reis
Bjarne Stroustrup
Jason Merrill
Hubert Tong
Eric Niebler
Casey Carter
Tom Honermann
Erich Keane
2018-06-23

Yet another approach for constrained declarations

Abstract

We propose a short syntax for the constrained declaration of function parameters, function return types and variables. The new syntax is a “constrained auto”, e.g. void sort(Sortable auto& c);.

Contents

  1. Revision history
  2. Proposal summary
  3. Part 1: “Constrained auto
  4. Part 2: Relaxed “constrained auto
  5. Part 3: Meaning of “template <Concept T>

Revision history

Proposal summary

This paper proposes three things:

  1. A syntax for constrained declarations that is practically a “constrained auto”; the principle being “wherever auto goes, a Constraint auto can also (non-recursively) go”. The semantics are to deduce like auto and additionally check a constraint. In a nutshell,
    void f(Sortable auto x);
    Sortable auto f();      // #1
    Sortable auto x = f();  // #2
    template <Sortable auto N> void f();
    and all combined:
    template <Sortable auto N> Sortable auto f(Sortable auto x)
    {
        Sortable auto y = init;
    }
    An unconstrained version of that is:
    template <auto N> auto f(auto x)
    {
        auto y = init;
    }
    So, this proposal includes auto-typed parameters for functions, which we already allow for lambdas.
  2. An additional relaxation where the auto is optional for the cases #1 and #2 illustrated above:
    Sortable f();
    Sortable x = f();
  3. Simplifying (and thus restricting) the rules in [temp.param]/10, so that template <Sortable S> always means that S is a type parameter, and template <Sortable auto S> always means that S is a non-type parameter. Template template-parameters are no longer supported in this short form. Moreover, Sortable is restricted to be a concept that takes a type parameter or type parameter pack; non-type and template concepts are no longer supported in this short form.

Sortable is a “type concept” in all the examples of this summary.

This paper specifically does not propose

The idea of this approach is to provide a syntax that

Part 1: “Constrained auto

The approach proposed here borrows a subset of P0807R0 An Adjective Syntax for Concepts. The idea is that we don’t try to come up with a notation that does everything that P0807 does; in particular, there is no proposal for a new syntax to introduce a type name.

Function templates

The approach is simple: allow auto parameters to produce function templates (as they produce polymorphic lambdas), and allow the auto to be preceded by a concept name. In every case, such a parameter is a deduced parameter, and we can see which parameters are deduced and which ones are not:

[](auto a, auto& b, const auto& c, auto&& d) {...}; // unconstrained
[](Constraint auto a, Constraint auto& b, const Constraint auto& c, Constraint auto&& d) {...}; // constrained

void f1(auto a, auto& b, const auto& c, auto&& d) {...}; // unconstrained
void f2(Constraint auto a, Constraint auto& b, const Constraint auto& c, Constraint auto&& d) {...}; // constrained

[](Constraint auto&& a, SomethingElse&& b) {...}; // a constrained deduced forwarding reference and a concrete rvalue reference
void f3(Constraint auto&& a, SomethingElse&& b) {...}; // a constrained deduced forwarding reference and a concrete rvalue reference

The appearance of auto (including Constraint auto) in a parameter list tells us that we are dealing with a function template. For each parameter, we know whether it is deduced or not. We can tell apart concepts from types: concepts precede auto, types do not.

Return types and variable declarations

Constrained return types work the same way:

auto f4();                  // unconstrained, deduced.

Constraint auto f5();       // constrained, deduced.

Whatever f6();              // See part 2. If Whatever is a type, not deduced.
                            // If Whatever is a concept, constrained and deduced.

Note that f4, f5 and f6 are not templates (whereas the previous f1, f2 and f3 are templates). Here, there is no mention of auto in the parameter list. Users have the choice of adopting a style where it is explicit as to whether the return type is deduced.

Constrained types for variables work the same way:

auto x1 = f1();             // unconstrained, deduced.

Constraint auto x2 = f2();  // constrained, deduced.

Whatever x3 = f3();         // See part 2. If Whatever is a type, not deduced.
                            // If Whatever is a concept, constrained and deduced.

Again, users can make it so that it is easy to see when deduction occurs.

Since non-type template parameters can be deduced via auto (as in template <auto N> void f();), we also allow a constraint there:

template <Constraint auto N> void f7();

Note, however, that this can only be a type constraint; non-type concepts (including auto concepts) are not allowed in this form.

Other uses of auto

In concert with the general approach that “Constraint auto goes wherever auto goes”, new-expressions and operators work:

auto alloc_next() { return new Sortable auto(this->next_val()); }

operator Sortable auto() { }

A “Constraint auto” cannot be used to indicate that a function declarator has a trailing return type:

Constraint auto f() -> auto; // ill-formed; shall be the single type-specifier auto

decltype(auto) can also be constrained:

auto f() -> Constraint decltype(auto);
Constraint decltype(auto) x = f();

Structured bindings do deduce auto in some cases; however, the auto is deduced from the whole (and not from the individual components). It is somewhat doubtful that applying the constraint to the whole, as opposed to (for example) applying separately to each component, is the correct semantic. Therefore, we propose to defer enabling the application of constraints to structured bindings to separate papers.

General rules

The constraint applies directly to the deduced type. It does not apply to the possibly cv-qualified type described by the type specifiers, nor does it apply to the type declared for the variable:

const Assignable<int> auto&& c = *static_cast<int *>(p); // Assignable<int &, int>

Naturally, if the deduced type is cv-qualified (or a reference), the constraint applies to that type.

To keep things simple, an auto being constrained is always immediately preceded by the constraint. So, cv-qualifiers and concept-identifiers cannot be freely mixed:

const Contraint auto x = foo(); // ok
Constraint const auto x = foo(); // ill-formed
Constraint auto const y = foo(); // ok

We propose only the ability to apply one single constraint for a parameter, return type, or non-type template parameter. Any proposal to consider multiple constraints should happen separately after C++20.

Partial concept identifiers also work. Given a concept template <typename T, typename... Args> concept Constructible = /* ... */;, we can say:

void f(Constructible<int> auto x);   // Constructible<decltype(x), int> is satisfied

Constructible<int> auto f();

Constructible<int> auto x = f();

template <Constructible<int> auto N> void f();

Part 2: Relaxed “constrained auto

In the return type of a function declaration, we can leave out the auto. So, in addition to

Constraint auto f1();

we can write

Constraint f2();

Neither f1 nor f2 are templates. It seems fairly reasonable to allow omitting the auto, but that is intended to be a relaxation of the general rule, not a replacement for Constraint auto.

In variable declarations, omitting the auto also seems reasonable:

Constraint x = f2();

Note, in particular, that we already have a syntax that does (partial) deduction but doesn’t make that explicit in the syntax:

std::tuple x = foo();

The variable case in particular seems reasonable, considering the already existing deduction syntaxes that don’t call attention to deduction. The user always has a choice to use a more explicit syntax. The return type case might well have a weaker rationale for being allowed. It should be noted, though, that this relaxation in general was present in the TS; this paper is merely not proposing it for parameters.

Certain disambiguation details need to be handled:

bool b(Constructible<int> && bar());         // variable definition

void foo() {
    Constructible<int> * f2();               // disambiguation/type-name interpretation rule required
    Constructible<int> * selector = f2();    // (same?) disambiguation/type-name interpretation rule required
}

Note that the variable definition is unambiguously a variable definition. This proposal proposes no function declaration syntax that would clash with it; in particular, Constructible<int> is not considered a type-name that is short for Constructible<int> auto except in limited contexts. It’s merely something to be aware of, and a demonstration of how the presence of auto avoids ambiguity. For each remaining case above, Constructible<int> does appear within such a limited context when parsing the statements as prospective declaration statements; we propose to generalize the rule for disambiguating in favor of declaration statements to cover this case.

Part 3: Meaning of “template <Concept T>

In [temp.param]/10 we have:

A constrained-parameter declares a template parameter whose kind (type, non-type, template) and type match that of the prototype parameter (17.6.8) of the concept designated by the qualified-concept-name in the constrained-parameter. Let X be the prototype parameter of the designated concept. The declared template parameter is determined by the kind of X (type, non-type, template) and the optional ellipsis in the constrained-parameter as follows.

[Example:

template<typename T> concept C1 = true;
template<template<typename> class X> concept C2 = true;
template<int N> concept C3 = true;
template<typename... Ts> concept C4 = true;
template<char... Cs> concept C5 = true;

template<C1 T> void f1();       // OK, T is a type template-parameter
template<C2 X> void f2();       // OK, X is a template with one type-parameter
template<C3 N> void f3();       // OK, N has type int
template<C4... Ts> void f4();   // OK, Ts is a template parameter pack of types
template<C4 T> void f5();       // OK, T is a type template-parameter
template<C5... Cs> void f6();   // OK, Cs is a template parameter pack of chars

end example]

Does that seem like a mouthful?

That’s because it is. In template <Constraint T>, the kind of T depends on the kind of the prototype parameter of Constraint.

We instead propose that, for such a constrained-parameter syntax:

To be clear, we are not proposing that concepts in general should not have non-type or template template parameters. We are merely proposing for it to be the case that the constrained parameter shortcut is not provided for concepts with such prototype parameters; such concepts would need to be used with a requires-clause. The constrained parameter syntax should mean just one thing. Note that the same syntax template <A T> is still a non-type parameter when A is a type name rather than a concept. We are willing to tolerate this small potential for ambiguity.

The rationale for this part is as follows:

  1. It seems desirable to have the constrained template parameter syntax.
  2. It would be nice if that syntax covered the most common case.
  3. It would further be nice if that syntax covered only the most common case.
  4. The other cases are expected to be so rare that there’s no need to provide a shortcut for them, and they are certainly rare enough that they shouldn’t use the same syntax.

So, to clarify:

Other use cases can be done with requires-clauses.