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


2681. Deducing member array type from string literal

Section: 12.2.2.9  [over.match.class.deduct]     Status: C++23     Submitter: Jonathan Caves     Date: 2020-03-17

[Accepted as a DR at the February, 2023 meeting.]

Consider:

  template<typename T, std::size_t N>
  struct A {
    T array[N];
  };

  A a = { "meow" };

The current wording says in 12.2.2.9 [over.match.class.deduct] bullet 1.8:

This will fail overload resolution, because a string literal (which is an lvalue) does not match a parameter of type T (&&)[N].

Proposed resolution (approved by CWG 2023-02-07):

  1. Change in 12.2.2.9 [over.match.class.deduct] paragraph 1.8 as follows:

    • if ei is of array type and xi is a braced-init-list or string-literal, Ti is an rvalue reference to the declared type of ei, and
    • if ei is of array type and xi is a string-literal, Ti is an lvalue reference to the const-qualified declared type of ei, and
    • ...
  2. Append to the example in 12.2.2.9 [over.match.class.deduct] paragraph 2 as follows:

      G g(true, 'a', 1); // OK, deduces G<char, bool>
    
      template<class T, std::size_t N>
      struct H {
        T array[N];
      };
      template<class T, std::size_t N>
      struct I {
        volatile T array[N];
      };
      template<std::size_t N>
      struct J {
        unsigned char array[N];
      };
    
      H h = { "abc" };  // OK, deduces H<char, 4> (not T = const char)
      I i = { "def" };  // OK, deduces I<char, 4>
      J j = { "ghi" };  // error: cannot bind reference to array of unsigned char to array of char in deduction