Title
basic_string needs deduction guides from basic_string_view
Status
c++20
Section
[basic.string][string.cons]
Submitter
Stephan T. Lavavej

Created on 2018-03-03.00:00:00 last changed 46 months ago

Messages

Date: 2018-03-18.16:03:30

Proposed resolution:

This wording is relative to N4727.

  1. Edit [basic.string], class template basic_string synopsis, as indicated:

    […]
    
    template<class InputIterator,
             class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
      basic_string(InputIterator, InputIterator, Allocator = Allocator())
        -> basic_string<typename iterator_traits<InputIterator>::value_type,
                        char_traits<typename iterator_traits<InputIterator>::value_type>,
                         Allocator>;
    
    template<class charT,
             class traits,
             class Allocator = allocator<charT>>
      explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
        -> basic_string<charT, traits, Allocator>;
    
    template<class charT,
             class traits,
             class Allocator = allocator<charT>>
      basic_string(basic_string_view<charT, traits>, typename see below::size_type, typename see below::size_type, 
                   const Allocator& = Allocator())
        -> basic_string<charT, traits, Allocator>;
    
    }                     
    

    -?- A size_type parameter type in a basic_string deduction guide refers to the size_type member type of the type deduced by the deduction guide.

  2. Edit [string.cons] as indicated:

    template<class InputIterator,
             class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
      basic_string(InputIterator, InputIterator, Allocator = Allocator())
        -> basic_string<typename iterator_traits<InputIterator>::value_type,
                        char_traits<typename iterator_traits<InputIterator>::value_type>,
                         Allocator>;
    

    -25- Remarks: Shall not participate in overload resolution if InputIterator is a type that does not qualify as an input iterator, or if Allocator is a type that does not qualify as an allocator ([container.requirements.general]).

    template<class charT,
             class traits,
             class Allocator = allocator<charT>>
      explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
        -> basic_string<charT, traits, Allocator>;
    
    template<class charT,
             class traits,
             class Allocator = allocator<charT>>
      basic_string(basic_string_view<charT, traits>, typename see below::size_type, typename see below::size_type, 
                   const Allocator& = Allocator())
        -> basic_string<charT, traits, Allocator>;                                          
    

    -?- Remarks: Shall not participate in overload resolution if Allocator is a type that does not qualify as an allocator ([container.requirements.general]).

Date: 2018-03-17.00:00:00

[ 2018-3-17 Adopted in Jacksonville ]

Date: 2018-03-17.15:56:41

[ 2018-14: Wednesday night issues processing: both this and 2946 to status "Immediate". ]

Date: 2018-03-03.00:00:00

The Proposed Resolution for LWG 2946 appears to be correct and we've implemented it in MSVC, but it worsens a pre-existing problem with basic_string class template argument deduction.

The following s1 and s2 compiled in C++17 before LWG 2946's PR, fail to compile after LWG 2946's PR, and are fixed by my PR:

basic_string s1("cat"sv);
basic_string s2("cat"sv, alloc);

The following s4 failed to compile in C++17, and is fixed by my PR:

// basic_string s3("cat"sv, 1, 1);
basic_string s4("cat"sv, 1, 1, alloc);

(s3 failed to compile in C++17, and would be fixed by my PR, but it is affected by a pre-existing and unrelated ambiguity which I am not attempting to fix here.)

As C++17 and LWG 2946's PR introduced templated constructors for basic_string from basic_string_view, we need to add corresponding deduction guides.

The constructors take const T& that's convertible to basic_string_view (the additional constraint about not converting to const charT* is irrelevant here). However, CTAD can't deduce charT and traits from arbitrary user-defined types, so the deduction guides need T to be exactly basic_string_view.

Additionally, we need to handle the size_type parameters in the same way that the unordered containers do. This PR has been implemented in MSVC.

History
Date User Action Args
2021-02-25 10:48:01adminsetstatus: wp -> c++20
2018-03-18 16:03:30adminsetmessages: + msg9756
2018-03-18 16:03:30adminsetstatus: immediate -> wp
2018-03-17 15:56:41adminsetmessages: + msg9722
2018-03-17 15:56:41adminsetstatus: new -> immediate
2018-03-04 13:02:22adminsetmessages: + msg9709
2018-03-03 00:00:00admincreate