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

2024-12-19


662. Forming a pointer to a reference type

Section: 13.10.3  [temp.deduct]     Status: NAD     Submitter: Alisdair Meredith     Date: 28 November 2007

The Standard currently specifies (9.2.4 [dcl.typedef] paragraph 9, 13.4.2 [temp.arg.type] paragraph 4) that an attempt to create a reference to a reference (via a typedef or template type parameter) is effectively ignored. The same is not true of an attempt to form a pointer to a reference; that is, assuming that T is specified to be a reference type,

    template <typename T> void f(T t) {
        T& tr = t;   // OK
        T* tp = &t;  // error
    }

It would be more consistent to allow pointers to references to collapse in the same way that references to references do.

Rationale (February, 2008):

In the absence of a compelling need, the CWG felt that it was better not to change the existing rules. Allowing this case could cause a quiet change to the meaning of a program, because attempting to create a pointer to a reference type is currently a deduction failure.

Additional discussion (May, 2009):

Consider the following slightly extended version of the example above:

    template <typename T> void f(T t) {
        T& tr = t;   // OK
        T* tp = &t;  // error
        auto * ap = &t; // OK!
    }

This means that a template that expects a reference type will need to use auto just to work around the failure to collapse pointer-to-reference types. The result might, in fact, be subtly different with auto, as well, in case there is an overloaded operator& function that doesn't return exactly T*. This contradicts one of the main goals of C++0x, to make it simpler, more consistent, and easier to teach.

Rationale (July, 2009):

The CWG reaffirmed its early decision. In general, templates will need to be written differently for reference and non-reference type parameters. Also, the Standard library provides a facility, std::remove_reference, that can be used easily for such cases.