This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 110c. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-02-07


625. Use of auto as a template-argument

Section: 9.2.9.6  [dcl.spec.auto]     Status: CD2     Submitter: John Spicer     Date: 9 March 2007

[Voted into WP at March, 2010 meeting.]

The auto specifier can be used only in certain contexts, as specified in 9.2.9.6 [dcl.spec.auto] paragraphs 2-3:

Otherwise (auto appearing with no type specifiers other than cv-qualifiers), the auto type-specifier signifies that the type of an object being declared shall be deduced from its initializer. The name of the object being declared shall not appear in the initializer expression.

This use of auto is allowed when declaring objects in a block (8.4 [stmt.block]), in namespace scope (6.4.6 [basic.scope.namespace]), and in a for-init-statement (8.6.4 [stmt.for]). The decl-specifier-seq shall be followed by one or more init-declarators, each of which shall have a non-empty initializer of either of the following forms:

It was intended that auto could be used only at the top level of a declaration, but it is not clear whether this wording is sufficient to forbid usage like the following:

    template <class T> struct A {};
    template <class T> void f(A<T> x) {}

    void g()
    {
        f(A<short>());

        A<auto> x = A<short>();
    }

Notes from the February, 2008 meeting:

It was agreed that the example should be ill-formed.

Proposed resolution (October, 2009):

Change 9.2.9.6 [dcl.spec.auto] paragraph 3 as follows:

...The auto shall appear as one of the decl-specifiers in the decl-specifier-seq and the decl-specifier-seq shall be followed by one or more init-declarators, each of which shall have a non-empty initializer.