Title
Local types, overload resolution, and template argument deduction
Status
cd1
Section
13.10.3 [temp.deduct]
Submitter
Mark Mitchell

Created on 2004-11-24.00:00:00 last changed 197 months ago

Messages

Date: 2008-06-15.00:00:00

[Voted into the WP at the June, 2008 meeting as paper N2657.]

Date: 2008-06-15.00:00:00

Notes from the June, 2008 meeting:

Paper N2657, adopted at the Sophia Antipolis (June, 2008) meeting, removed the restriction against local and unnamed types as template parameters. The example is now well-formed.

Date: 2007-04-15.00:00:00

Notes from the April, 2007 meeting:

The Evolution Working Group is now actively pursuing an extension that would allow local and/or nameless types to be used as template arguments, so this resolution will be held in abeyance until the outcome of that proposal is known.

Date: 2005-12-15.00:00:00

Additional note (December, 2005):

The Evolution Working Group is currently considering an extension that would effectively give linkage to some (but perhaps not all) types that currently have no linkage. If the proposed resolution above is adopted and then later a change along the lines that the EWG is considering were also adopted, the result would be a silent change in the result of overload resolution, because the newly-acceptable specializations would become part of the overload set. It is not clear whether that possibility is sufficient reason to delay adoption of this resolution or not.

Date: 2005-10-15.00:00:00

Proposed resolution (October, 2005):

Add a new sub-bullet following bullet 3, sub-bullet 7 ("Attempting to give an invalid type to a non-type template parameter") of 13.10.3 [temp.deduct] paragraph 2:

  • Attempting to use a local or unnamed type as the value of a template type parameter.

Date: 2005-04-15.00:00:00

Notes from the April, 2005 meeting:

The question in the original example was whether there should be an error, even though the uninstantiable template was not needed for calling the best-matching function. The broader issue is whether a user would prefer to get an error or to call a “worse” non-template function in such cases. For example:

    template<typename T> void f(T);
    void f(int);
    void g() {
        enum E { e };
        f(e);    // call f(int) or get an error?
    }

It was observed that the type deduction rules are intended to model, albeit selectively, the other rules of the language. This would argue in favor of the second approach, a type-deduction failure, and the consensus of the group was that the incremental benefit of other approaches was not enough to outweigh the additional complexity of specification and implementation.

Date: 2004-11-24.00:00:00

It is not clear how to handle the following example:

    struct S {
        template <typename T> S(const T&);
    };
    void f(const S&);
    void f(int);
    void g() {
        enum E { e };
        f(e);    // ill-formed?
    }

Three possibilities suggest themselves:

  1. Fail during overload resolution. In order to perform overload resolution for the call to f, the declaration of the required specialization of the S constructor must be instantiated. This instantiation uses a local type and is thus ill-formed (13.4.2 [temp.arg.type] paragraph 2) , rendering the example as a whole ill-formed, as well.

  2. Treat this as a type-deduction failure. Although it is not listed currently among the causes of type-deduction failure in 13.10.3 [temp.deduct] paragraph 2, it could plausibly be argued that instantiating a function declaration with a local type as a template type-parameter falls under the rubric of “If a substitution in a template parameter or in the function type of the function template results in an invalid type” and thus should be a type-deduction failure. The result would be that the example is well-formed because f(const S&) would be removed from the list of viable functions.

  3. Fail only if the function selected by overload resolution requires instantiation with a local type. This approach would require that the diagnostic resulting from the instantiation of the function type during overload resolution be suppressed and either regenerated or regurgitated once overload resolution is complete. (The example would be well-formed under this approach because f(int) would be selected as the best match.)

(See also issue 489.)

History
Date User Action Args
2008-10-05 00:00:00adminsetstatus: dr -> cd1
2008-06-29 00:00:00adminsetmessages: + msg1716
2008-06-29 00:00:00adminsetmessages: + msg1715
2008-06-29 00:00:00adminsetstatus: review -> dr
2007-05-06 00:00:00adminsetmessages: + msg1484
2007-05-06 00:00:00adminsetstatus: ready -> review
2006-11-05 00:00:00adminsetstatus: review -> ready
2006-04-22 00:00:00adminsetstatus: ready -> review
2005-12-16 00:00:00adminsetmessages: + msg1290
2005-10-22 00:00:00adminsetmessages: + msg1226
2005-10-22 00:00:00adminsetstatus: drafting -> ready
2005-05-01 00:00:00adminsetmessages: + msg1167
2005-05-01 00:00:00adminsetstatus: open -> drafting
2004-11-24 00:00:00admincreate