Template Argument Type Deduction

Document number: N4469
Date: 2015-04-10
Project: Programming Language C++, Evolution Working Group
Revises: N3601
Reply-to: James Touton <bekenn@gmail.com>
Mike Spertus, Symantec <mike_spertus@symantec.com>

Table of Contents

  1. Table of Contents
  2. Introduction
  3. Motivation and Scope
  4. Impact On the Standard
  5. Design Decisions
  6. Compare and contrast: auto vs implicit template parameters
  7. Technical Specifications
  8. Wording
  9. Acknowledgments

Introduction

This paper proposes allowing the types of value arguments (constants) passed to templates to be automatically deduced. Currently, the type of a value parameter must be explicitly specified, which leads to unnecessary verboseness and reduced flexibility when writing a template intended to take constant arguments of any type. Example:

template <typename T, T v> struct S;    // declaration
S<decltype(x), x> s;                    // instantiation

The example makes use of decltype to retrieve the type of x (a compile-time constant) before passing both the type and the value of x to S. This is a common pattern, with over 100,000 hits on Google. The goal is to be able to modify the declaration of S such that the type of x doesn't need to be passed as a separate template argument, resulting in this simpler instantiation:

S<x> s; // desired instantiation

Two different approaches are presented here:

Which approach is more appropriate depends on the definition of the template. Implicit template parameters provide names for the deduced types; if such a name is needed, then implicit template parameters are appropriate. If only the constant value is used, then auto provides a simpler and more straightforward syntax.

Motivation and Scope

Consider a generic function call logger for an application that provides callback function pointers to a library. The logger should print the name of the function, the argument values, and the result of a call to any callback. In order to avoid calling the logger from within a callback function (and thus having to modify each function to support the logger), the logger itself is passed to the library in place of the callback function, and the logger passes the arguments along to the callback function. This implies that the logger for a callback function must match the callback function's type so that the library can call it directly.

It is desirable that the instantiation syntax for the logger be simple; the following seems perfectly reasonable:

// can't specify string literals as template arguments, so provide a character array instead
static constexpr char cbname[] = "my_callback";
void initialize()
{
    library::register_callback(logger<my_callback, cbname>);
}

In order for this to work, logger must be a template that takes a function pointer and a character pointer as arguments. If the type of the function is fixed, this is no problem:

// log any function with the signature int(int)
template <int (* f)(int), const char* name>
int logger(int arg)
{
    cout << name << '(' << arg << ')';
    int result = f(arg);
    cout << " -> " << result << endl;
    return result;
}

If the type of the function is not fixed, things get more complicated:

// log each argument in a comma-separated list
template <class... Args> void log_args(Args... args);

// struct template that accepts a function pointer and a name
template <class F, F f, const char* name> struct fn_logger;

// use partial specialization to constrain f
// note that a second specialization would be needed to support functions returning void
template <class R, class... Args, R (* f)(Args...), const char* name>
struct fn_logger<f, name>
{
    // call f, logging arguments and result
    static R call(Args... args)
    {
        cout << name << '(';
        log_args(args...);
        cout << ')';
        auto result = f(args...);
        cout << " -> " << result << endl;
        return result;
    }
};

// variable template to simplify use of fn_logger
template <class F, F f, const char* name> constexpr auto logger = fn_logger<F, f, name>::call;

The instantiation syntax also gets more complicated, because the type of the function must be passed as an additional argument:

// can't specify string literals as template arguments, so provide a character array instead
static constexpr char cbname[] = "my_callback";
void initialize()
{
    library::register_callback(decltype(&my_callback), logger<my_callback, cbname>);
}

Using auto

The template parameter list syntax can be extended in a simple and natural way using the auto keyword to indicate that the type of a value parameter is deduced at the point of instantiation:

template <auto x> constexpr auto constant = x;

auto v1 = constant<5>;      // v1 == 5, decltype(v1) is int
auto v2 = constant<true>;   // v2 == true, decltype(v2) is bool
auto v3 = constant<'a'>;    // v3 == 'a', decltype(v3) is char

The usual type modifiers may be used to constrain the type of the value parameter without the use of partial specialization:

// p must be a pointer to const something
template <const auto* p> struct S;

Partial specialization may be used to switch on the type of a value parameter:

template <auto x> struct S;
template <int n>
struct S<n>
{
    const char* type_name = "int";
};

Here is what the logger would look like using auto:

// log each argument in a comma-separated list
template <class... Args> void log_args(Args... args);

// struct template that accepts a function pointer and a name
template <auto f, const char* name> struct fn_logger;

// use partial specialization to constrain f
// note that a second specialization would be needed to support functions returning void
template <class R, class... Args, R (* f)(Args...), const char* name>
struct fn_logger<f, name>
{
    // call f, logging arguments and result
    static R call(Args... args)
    {
        cout << name << '(';
        log_args(args...);
        cout << ')';
        auto result = f(args...);
        cout << " -> " << result << endl;
        return result;
    }
};

// variable template to simplify use of fn_logger
template <auto f, const char* name> constexpr auto logger = fn_logger<f, name>::call;

The function type no longer needs to be explicitly specified, which means the instantiation can go back to the desired form:

library::register_callback(logger<my_callback, cbname>);

Using implicit template parameters

The logger definition can be simplified through the use of implicit template parameters. Because such parameters have names, the types can be referenced throughout the template definition without the aid of decltype. Further, since implicit parameters are inferred and never specified at the point of instantiation, they can be used to obviate the need for partial specialization. Using implicit template parameters, the logger code becomes considerably shorter:

// as before
template <class... Args> void log_args(Args... args);

// primary template -- no need for partial specialization or the variable template
// a second version would be needed to support functions returning void
template <using class R, using class... Args, R (* f)(Args...), const char* name>
R logger(Args... args)
{
    cout << name << '(';
    log_args(args...);
    cout << ')';
    auto result = f(args...);
    cout << " -> " << result << endl;
    return result;
}

Just as with auto, partial specialization may be used to switch on the type of a value parameter:

template <using typename T, T x> struct S;
template <int n>
struct S<n>
{
    const char* type_name = "int";
};

As variadic template parameters

When auto appears as the type specifier for a parameter pack, it signifies that the type for each corresponding argument should be independently deduced:

// List of heterogeneous constant values
// same as template <auto v1, auto v2, auto v3, ...>
template <auto... vs> struct value_list { };

// Retrieve the nth value in a list of values
template <size_t n, auto... vs> struct nth_value;
template <size_t n, auto v1, auto... vs>
struct nth_value<n, v1, vs...>
{
    static constexpr auto value = nth_value<n - 1, vs...>::value;
};
template <auto v1, auto... vs>
struct nth_value<0, v1, vs...>
{
    static constexpr auto value = v1;
};

A list of homogeneous constant values can be constructed with the aid of decltype:

// List of homogeneous constant values
template <auto v1, decltype(v1)... vs> struct typed_value_list { };

Implicit template parameters can also be used to generate either kind of list:

// List of heterogeneous constant values
template <using typename... Ts, Ts... vs> struct value_list { };

// List of homogeneous constant values
template <using typename T, T... vs> struct typed_value_list { };

Note that the homogeneous list can be empty only if a default type is provided for T:

// possibly empty
template <using typename T = void, T... vs> struct typed_value_list { };

Impact On the Standard

The proposed features add no keywords and do not change the meaning of any existing code.

Opportunity cost

There is an opportunity cost associated with adopting this particular meaning for the auto keyword in this context. It has been suggested that auto could be used to allow for template parameters accepting any kind of template argument, be it a type, a value, a template, or any other construct that templates may accept at any point in the future.

Such a feature is desirable, but the use of the auto keyword for it is not. There is no existing context in which auto acts as anything other than a stand-in for a type name; consistency with the rest of the language dictates that auto behave as spelled out in this paper.

Design Decisions

Implicit template parameters

The syntax used in the examples for implicit template parameters has an issue with positional consistency. Implicit template parameters occupy space within the angle brackets of a template declaration. At a casual glance, they look like ordinary template parameters. This leads to an inconsistency between the positions of parameters in the template parameter list and the positions of corresponding arguments at the instantiation site:

template <using typename T, T x> constexpr auto constant = x;
constant<42> ltue;  // 42 is the first argument, but supplies
                    // a value for the second parameter

This gets worse as more implicit parameters are added:

// declaration
template <using typename T1, T1 x1,
          using typename T2, T2 x2>
struct value_pair;

// instantiation
value_pair<'a', 1> values;

To address this issue, an alternative syntax is presented here that brings the implicit parameters out of the parameter list:

template using typename T1, typename T2 <T1 x1, T2 x2>
struct value_pair;

In addition to fixing the positional consistency problem, this syntax has the advantage of being shorter than the syntax originally proposed in N3601 because the keyword using doesn't need to be repeated for each parameter. If this syntax is adopted, a better name for the feature might be "template meta-parameters" (instead of "implicit template parameters").

auto

A few people have suggested that all values in a parameter pack introduced by auto should have the same type. The rationale seems to be that because auto can be replaced by a single type name in a multiple variable definition, the same should be true here:


auto x = 3.5, y = "hello";  // error, x and y must have the same type

This approach is comparatively inflexible, in that it does not allow variadic lists of heterogeneous values. Additionally, the behavior specified in this document mirrors the existing behavior of the typename and class keywords in this context:

// same as template <typename T1, typename T2, typename T3, ...>
template <typename... Ts> struct type_list { };

// same as template <auto v1, auto v2, auto v3, ...>
template <auto... vs> struct value_list { };

Compare and contrast: auto vs implicit template parameters

There is no use of auto as proposed in this paper that couldn't also be achieved with implicit template parameters. However, auto brings with it significant syntax advantages that should justify its adoption even in the presence of implicit template parameters:

Advantages of auto

Shorter syntax

The shorter syntax afforded by the auto keyword is an aid to readability.

Leverages existing meaning of auto

The auto keyword already has a well-understood meaning in variable and parameter declaration contexts; this proposal simply imports that meaning into template parameter declarations. This implies that a programmer generally familiar with C++ but unfamiliar with this specific feature should have no trouble understanding it at first sight.

Advantages of implicit template parameters

Implicit template parameters offer greater flexibility by allowing the programmer to specify a name for the deduced type. This simplifies more complex template definitions by allowing the programmer to use the deduced type name in the template definition.

Why not both?

Thankfully, the two features can coexist; programmers can (and should) use auto whenever they don't need a name for the deduced type, while also using implicit template parameters when the type name is used elsewhere in the template definition. This combination maximizes both readability and flexibility.

Technical Specifications

Wording

All modifications are presented relative to N4296.

auto

Modify §7.1.6.4 [dcl.spec.auto] paragraph 5:

A placeholder type can also be used in declaring a variable in the condition of a selection statement (6.4) or an iteration statement (6.5), in the type-specifier-seq in the new-type-id or type-id of a new-expression (5.3.4), in a for-range-declaration, and in declaring a static data member with a brace-or-equal-initializer that appears within the member-specification of a class definition (9.4.2), and in the decl-specifier-seq in the parameter-declaration of a template-parameter (14.1).

Modify §14.1 [temp.param] paragraph 4:

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

Modify §14.3.2 [temp.arg.nontype] paragraph 1:

A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter. If the type of the template-parameter is declared using a placeholder type (7.1.6.4, 14.1), the deduced parameter type is determined from the type of the template-argument using the rules for template argument deduction from a function call (14.8.2.1). If a deduced parameter type is not permitted for a template-parameter declaration (14.1), the program is ill-formed. For a non-type template-parameter of reference or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):

[ Note: If the template-argument represents a set of overloaded functions (or a pointer or member pointer to such), the matching function is selected from the set (13.4). —end note ]

Modify §14.3.2 [temp.arg.nontype] paragraph 2:

[ Example:

template<const int* pci> struct X { /* ... */ };
int ai[10];
X<ai> xi; // array to pointer and qualification conversions

struct Y { /* ... */ };
template<const Y& b> struct Z { /* ... */ };
Y y;
Z<y> z; // no conversion, but note extra cv-qualification

template<int (&pa)[5]> struct W { /* ... */ };
int b[5];
W<b> w; // no conversion

void f(char);
void f(int);

template<void (*pf)(int)> struct A { /* ... */ };
A<&f> a; // selects f(int)

template<auto n> struct B { /* ... */ };
B<5> b1;   // OK: template parameter type is int
B<'a'> b2; // OK: template parameter type is char
B<2.5> b3; // error: template parameter type cannot be double

end example ]

Implicit template parameters

Option 1: N3601 syntax

Modify §14 [temp] paragraph 1:

A template defines a family of classes or functions or an alias for a family of types.

template-declaration:
template < template-parameter-list > declaration
template-parameter-list:
usingopt template-parameter
template-parameter-list , template-parameter

Insert a new paragraph after §14.1 [temp.param] paragraph 1:

A template-parameter that begins with the using keyword is an implicit template parameter. A template-parameter that is not an implicit template parameter is a normal template parameter. The value of an implicit template parameter cannot be specified explicitly; it is always deduced at the point of template instantiation.

Insert a new paragraph after §14.1 [temp.param] paragraph 14:

The program is ill-formed if the template-parameter-list of a template template-parameter contains an implicit template parameter.

Modify §14.3 [temp.arg] paragraph 1:

There are three forms of template-argument, corresponding to the three forms of template-parameter: type, non-type and template. The type and form of each template-argument specified in a template-id shall match the type and form specified for the corresponding normal template parameter declared by the template in its template-parameter-list, after deducing the values of any implicit template parameters.. When the parameter declared by the template is a template parameter pack (14.5.3), it will correspond to zero or more template-arguments.

Insert a new paragraph after §14.3 [temp.arg] paragraph 1:

If the definition of a normal template parameter depends on the value of an implicit template parameter, then the value of the implicit template parameter is deduced from the corresponding template-argument using the rules for template argument deduction from a function call (14.8.2.1). If the deduced type of a non-type template-parameter is not permitted for a template-parameter declaration (14.1), the program is ill-formed. If, during template instantiation, the value of any implicit template parameter is not deduced, the program is ill-formed.

Modify §14.3.3 [temp.arg.template] paragraph 3:

A template-argument matches a template template-parameter P when each of the normal template parameters in the template-parameter-list of the template-argument's corresponding class template or alias template A matches the corresponding template parameter in the template-parameter-list of P. Two template parameters match if, after deducing the values of any implicit template parameters, they are of the same kind (type, non-type, template),; for non-type template-parameters, their types are equivalent (14.5.6.1),; and for template template-parameters, each of their corresponding template-parametersnormal template parameters matches, recursively. When P's template-parameter-list contains a template parameter pack (14.5.3), the template parameter pack will match zero or more template parameters or template parameter packs in the template-parameter-listnormal template parameters of A with the same type and form as the template parameter pack in P (ignoring whether those template parameters are template parameter packs).

Option 2: Template metaparameter syntax

Modify §14 [temp] paragraph 1:

A template defines a family of classes or functions or an alias for a family of types.

template-declaration:
template template-metaparameter-clauseopt < template-parameter-list > declaration
template-metaparameter-clause:
using template-parameter-list
template-parameter-list:
template-parameter
template-parameter-list , template-parameter

Insert a new paragraph after §14.1 [temp.param] paragraph 1:

A template-parameter that appears in a template-metaparameter-clause is a template metaparameter. A template-parameter that is not a template metaparameter is a normal template parameter. The value of a template metaparameter cannot be specified explicitly; it is always deduced at the point of template instantiation.

Insert a new paragraph after §14.1 [temp.param] paragraph 13:

When parsing a default template-argument for a non-type template metaparameter, the first non-nested < is taken as the end of the template-parameter-list rather than a less-than operator.

Modify §14.3 [temp.arg] paragraph 1:

There are three forms of template-argument, corresponding to the three forms of template-parameter: type, non-type and template. The type and form of each template-argument specified in a template-id shall match the type and form specified for the corresponding normal template parameter declared by the template in its template-parameter-list, after deducing the values of any template metaparameters.. When the parameter declared by the template is a template parameter pack (14.5.3), it will correspond to zero or more template-arguments.

Insert a new paragraph after §14.3 [temp.arg] paragraph 1:

If the definition of a normal template parameter depends on the value of a template metaparameter, then the value of the template metaparameter is deduced from the corresponding template-argument using the rules for template argument deduction from a function call (14.8.2.1). If the deduced type of a non-type template-parameter is not permitted for a template-parameter declaration (14.1), the program is ill-formed. If, during template instantiation, the value of any template metaparameter is not deduced, the program is ill-formed.

Modify §14.3.3 [temp.arg.template] paragraph 3:

A template-argument matches a template template-parameter P when each of the template parameters in the template-parameter-list of the template-argument's corresponding class template or alias template A matches the corresponding template parameter in the template-parameter-list of P. Two template parameters match if, after deducing the values of any template metaparameters, they are of the same kind (type, non-type, template),; for non-type template-parameters, their types are equivalent (14.5.6.1),; and for template template-parameters, each of their corresponding template-parametersnormal template parameters matches, recursively. When P's template-parameter-list contains a template parameter pack (14.5.3), the template parameter pack will match zero or more template parameters or template parameter packs in the template-parameter-list of A with the same type and form as the template parameter pack in P (ignoring whether those template parameters are template parameter packs).

Wording common to both options 1 and 2

Modify §14.1 [temp.param] paragraph 11:

If a template-parameternormal template parameter of a class template or alias template has a default template-argument, each subsequent template-parameternormal template parameter shall either have a default template-argument supplied or be a template parameter pack. If a template-parameternormal template parameter of a primary class template or alias template is a template parameter pack, it shall be the last template-parameternormal template parameter. A normal template parameter that is a template parameter pack of a function template shall not be followed by another normal template parameter unless that template parameter can be deduced from the parameter-type-list of the function template or has a default argument (14.8.2).

Modify §14.1 [temp.param] paragraph 13:

When parsing a default template-argument for a non-type template-parameternormal template parameter, the first non-nested > is taken as the end of the template-parameter-list rather than a greater-than operator.

Modify §14.3 [temp.arg] paragraph 7:

When the template in a template-id is an overloaded function template, both non-template functions in the overload set and function templates in the overload set for which the template-arguments do not match the template-parametersnormal template parameters are ignored. If none of the function templates have matching template-parametersnormal template parameters, the program is ill-formed.

Acknowledgments

Mike Spertus and Daveed Vandevoorde developed the implicit template parameter concept and initial syntax (Option 1 above) in N3601.

Numerous people gave constructive feedback regarding the use of auto in template parameter lists in an isocpp.org discussion thread.