Title
Deducing member array type from string literal
Status
c++23
Section
12.2.2.9 [over.match.class.deduct]
Submitter
Jonathan Caves

Created on 2020-03-17.00:00:00 last changed 9 months ago

Messages

Date: 2023-02-18.18:43:04

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
    
Date: 2023-02-15.00:00:00

[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:

  • 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
  • ...

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

History
Date User Action Args
2023-07-16 13:00:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: dr -> open
2023-02-18 18:43:04adminsetstatus: ready -> dr
2023-02-10 23:01:52adminsetstatus: tentatively ready -> ready
2023-02-07 17:41:05adminsetstatus: review -> tentatively ready
2023-01-28 21:44:57adminsetstatus: open -> review
2023-01-14 22:16:14adminsetmessages: + msg7132
2020-03-17 00:00:00admincreate