Strange syntax for a basic_string constructor

40 views
Skip to first unread message

Andrey Semashev

unread,
Apr 12, 2018, 3:03:07 PM4/12/18
to ISO C++ Standard - Discussion
Hi,

I've stumbled upon this syntax in std::basic_string definition (N4659 or
N4727, [basic.string]/5):

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>;

(sorry if I got the formatting wrong.)

This is given in namespace scope, after the std::basic_string class
template definition.

I'm failing to recognize this syntax. It looks like it should be the
constructor from a pair of iterators, but (a) it is missing the
template<...> prefix for the basic_string template parameters, (b) it is
missing the basic_string<...>:: scope prefix, (c) it uses the trailing
result type syntax, which I don't think is legal for constructors (is
it?) and (d) it has no body and it is not defaulted or deleted.

Is this something new in the language that I'm missing?

If so, why is this definition (is it a definition?) duplicated while
there is an equivalent declaration in the basic_string definition?

Thanks.

Nevin Liber

unread,
Apr 12, 2018, 3:08:02 PM4/12/18
to std-dis...@isocpp.org
On Thu, Apr 12, 2018 at 2:03 PM, Andrey Semashev <andrey....@gmail.com> wrote:
Hi,

I've stumbled upon this syntax in std::basic_string definition (N4659 or N4727, [basic.string]/5):

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>;

(sorry if I got the formatting wrong.)

This is given in namespace scope, after the std::basic_string class template definition.

I'm failing to recognize this syntax. It looks like it should be the constructor from a pair of iterators, but (a) it is missing the template<...> prefix for the basic_string template parameters, (b) it is missing the basic_string<...>:: scope prefix, (c) it uses the trailing result type syntax, which I don't think is legal for constructors (is it?) and (d) it has no body and it is not defaulted or deleted.

Is this something new in the language that I'm missing?

I believe this is a template deduction guide, which maps a constructor signature to the template it should instantiate.  More details at [temp.deduct.guide].
--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com>  +1-847-691-1404
Reply all
Reply to author
Forward
0 new messages