Skip to content

Commit 6d9f750

Browse files
committed
Implement deduction guide for basic_string as described in P0433
llvm-svn: 324569
1 parent c19aed9 commit 6d9f750

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

libcxx/include/string

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ public:
311311
bool __invariants() const;
312312
};
313313
314+
template<class InputIterator,
315+
class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
316+
basic_string(InputIterator, InputIterator, Allocator = Allocator())
317+
-> basic_string<typename iterator_traits<InputIterator>::value_type,
318+
char_traits<typename iterator_traits<InputIterator>::value_type>,
319+
Allocator>; // C++17
320+
314321
template<class charT, class traits, class Allocator>
315322
basic_string<charT, traits, Allocator>
316323
operator+(const basic_string<charT, traits, Allocator>& lhs,
@@ -1485,6 +1492,18 @@ private:
14851492
friend basic_string operator+<>(const basic_string&, value_type);
14861493
};
14871494

1495+
#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1496+
template<class _InputIterator,
1497+
class _CharT = typename iterator_traits<_InputIterator>::value_type,
1498+
class _Allocator = allocator<_CharT>,
1499+
class = typename enable_if<__is_input_iterator<_InputIterator>::value, void>::type,
1500+
class = typename enable_if<__is_allocator<_Allocator>::value, void>::type
1501+
>
1502+
basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1503+
-> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
1504+
#endif
1505+
1506+
14881507
template <class _CharT, class _Traits, class _Allocator>
14891508
inline _LIBCPP_INLINE_VISIBILITY
14901509
void
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is dual licensed under the MIT and the University of Illinois Open
6+
// Source Licenses. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
// <string>
11+
// UNSUPPORTED: c++98, c++03, c++11, c++14
12+
13+
// template<class InputIterator,
14+
// class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
15+
// basic_string(InputIterator, InputIterator, Allocator = Allocator())
16+
// -> basic_string<typename iterator_traits<InputIterator>::value_type,
17+
// char_traits<typename iterator_traits<InputIterator>::value_type>,
18+
// Allocator>;
19+
//
20+
// The deduction guide shall not participate in overload resolution if InputIterator
21+
// is a type that does not qualify as an input iterator, or if Allocator is a type
22+
// that does not qualify as an allocator.
23+
24+
25+
#include <string>
26+
#include <iterator>
27+
#include <cassert>
28+
#include <cstddef>
29+
30+
#include "test_macros.h"
31+
32+
class NotAnItertor {};
33+
34+
template <typename T>
35+
struct NotAnAllocator { typedef T value_type; };
36+
37+
int main()
38+
{
39+
{ // Not an iterator at all
40+
std::basic_string s1{NotAnItertor{}, NotAnItertor{}, std::allocator<char>{}}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}}
41+
}
42+
{ // Not an input iterator
43+
const char16_t* s = u"12345678901234";
44+
std::basic_string<char16_t> s0;
45+
std::basic_string s1{std::back_insert_iterator(s0), // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}}
46+
std::back_insert_iterator(s0),
47+
std::allocator<char16_t>{}};
48+
}
49+
{ // Not an allocator
50+
const wchar_t* s = L"12345678901234";
51+
std::basic_string s1{s, s+10, NotAnAllocator<wchar_t>{}}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}}
52+
}
53+
54+
}

libcxx/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
// basic_string(InputIterator begin, InputIterator end,
1414
// const Allocator& a = Allocator());
1515

16+
// template<class InputIterator,
17+
// class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
18+
// basic_string(InputIterator, InputIterator, Allocator = Allocator())
19+
// -> basic_string<typename iterator_traits<InputIterator>::value_type,
20+
// char_traits<typename iterator_traits<InputIterator>::value_type>,
21+
// Allocator>;
22+
//
23+
// The deduction guide shall not participate in overload resolution if InputIterator
24+
// is a type that does not qualify as an input iterator, or if Allocator is a type
25+
// that does not qualify as an allocator.
26+
27+
1628
#include <string>
1729
#include <iterator>
1830
#include <cassert>
@@ -116,4 +128,48 @@ int main()
116128
test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A());
117129
}
118130
#endif
131+
132+
// Test deduction guides
133+
#if TEST_STD_VER > 14
134+
{
135+
const char* s = "12345678901234";
136+
std::basic_string s1{s, s+10, std::allocator<char>{}};
137+
using S = decltype(s1); // what type did we get?
138+
static_assert(std::is_same_v<S::value_type, char>, "");
139+
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
140+
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
141+
assert(s1.size() == 10);
142+
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
143+
}
144+
{
145+
const wchar_t* s = L"12345678901234";
146+
std::basic_string s1{s, s+10, test_allocator<wchar_t>{}};
147+
using S = decltype(s1); // what type did we get?
148+
static_assert(std::is_same_v<S::value_type, wchar_t>, "");
149+
static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
150+
static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
151+
assert(s1.size() == 10);
152+
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
153+
}
154+
{
155+
const char16_t* s = u"12345678901234";
156+
std::basic_string s1{s, s+10, min_allocator<char16_t>{}};
157+
using S = decltype(s1); // what type did we get?
158+
static_assert(std::is_same_v<S::value_type, char16_t>, "");
159+
static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
160+
static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
161+
assert(s1.size() == 10);
162+
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
163+
}
164+
{
165+
const char32_t* s = U"12345678901234";
166+
std::basic_string s1{s, s+10, explicit_allocator<char32_t>{}};
167+
using S = decltype(s1); // what type did we get?
168+
static_assert(std::is_same_v<S::value_type, char32_t>, "");
169+
static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
170+
static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
171+
assert(s1.size() == 10);
172+
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
173+
}
174+
#endif
119175
}

libcxx/www/cxx1z_status.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ <h3>Paper Status</h3>
150150
<tr><td><a href="https://wg21.link/P0298R3">P0298R3</a></td><td>CWG</td><td>A byte type definition</td><td>Kona</td><td>Complete</td><td>5.0</td></tr>
151151
<tr><td><a href="https://wg21.link/P0317R1">P0317R1</a></td><td>LWG</td><td>Directory Entry Caching for Filesystem</td><td>Kona</td><td></td><td></td></tr>
152152
<tr><td><a href="https://wg21.link/P0430R2">P0430R2</a></td><td>LWG</td><td>File system library on non-POSIX-like operating systems</td><td>Kona</td><td></td><td></td></tr>
153-
<tr><td><a href="https://wg21.link/P0433R2">P0433R2</a></td><td>LWG</td><td>Toward a resolution of US7 and US14: Integrating template deduction for class templates into the standard library</td><td>Kona</td><td></td><td></td></tr>
153+
<tr><td><a href="https://wg21.link/P0433R2">P0433R2</a></td><td>LWG</td><td>Toward a resolution of US7 and US14: Integrating template deduction for class templates into the standard library</td><td>Kona</td><td><i>In progress</i></td><td>7.0</td></tr>
154154
<tr><td><a href="https://wg21.link/P0452R1">P0452R1</a></td><td>LWG</td><td>Unifying &lt;numeric&gt; Parallel Algorithms</td><td>Kona</td><td></td><td></td></tr>
155155
<tr><td><a href="https://wg21.link/P0467R2">P0467R2</a></td><td>LWG</td><td>Iterator Concerns for Parallel Algorithms</td><td>Kona</td><td></td><td></td></tr>
156156
<tr><td><a href="https://wg21.link/P0492R2">P0492R2</a></td><td>LWG</td><td>Proposed Resolution of C++17 National Body Comments for Filesystems</td><td>Kona</td><td></td><td></td></tr>
@@ -161,7 +161,7 @@ <h3>Paper Status</h3>
161161
<tr><td><a href="https://wg21.link/P0574R1">P0574R1</a></td><td>LWG</td><td>Algorithm Complexity Constraints and Parallel Overloads</td><td>Kona</td><td></td><td></td></tr>
162162
<tr><td><a href="https://wg21.link/P0599R1">P0599R1</a></td><td>LWG</td><td>noexcept for hash functions</td><td>Kona</td><td>Complete</td><td>5.0</td></tr>
163163
<tr><td><a href="https://wg21.link/P0604R0">P0604R0</a></td><td>LWG</td><td>Resolving GB 55, US 84, US 85, US 86</td><td>Kona</td><td></td><td></td></tr>
164-
<tr><td><a href="https://wg21.link/P0607R0">P0607R0</a></td><td>LWG</td><td>Inline Variables for the Standard Library</td><td>Kona</td><td>In Progress</td><td>6.0</td></tr>
164+
<tr><td><a href="https://wg21.link/P0607R0">P0607R0</a></td><td>LWG</td><td>Inline Variables for the Standard Library</td><td>Kona</td><td><i>In Progress</i></td><td>6.0</td></tr>
165165
<tr><td><a href="https://wg21.link/P0618R0">P0618R0</a></td><td>LWG</td><td>Deprecating &lt;codecvt&gt;</td><td>Kona</td><td></td><td></td></tr>
166166
<tr><td><a href="https://wg21.link/P0623R0">P0623R0</a></td><td>LWG</td><td>Final C++17 Parallel Algorithms Fixes</td><td>Kona</td><td></td><td></td></tr>
167167
<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>
@@ -172,6 +172,7 @@ <h3>Paper Status</h3>
172172
</table>
173173

174174
<p><i>The parts of P0607 that are not done are the &lt;regex&gt; bits.</i></p>
175+
<p><i>So far, only the &lt;string&gt; part of P0433 has been implemented.</i></p>
175176

176177
<p><i>[ Note: "Nothing to do" means that no library changes were needed to implement this change -- end note]</i></p>
177178

0 commit comments

Comments
 (0)