From 6d82c56288f1d7048a39427a827fe337c773f0f5 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 7 Jun 2017 13:35:08 +0100 Subject: [PATCH] Add C++17 deduction guide for std::basic_string (P0433R2, partial) * include/bits/alloc_traits.h (__is_allocator, _RequireAllocator): New trait and alias for detecting Allocator-like types. * include/bits/basic_string.h (basic_string): Add deduction guide from P0433. * include/ext/alloc_traits.h (__gnu_cxx::__alloc_traits): Add template parameter with default template argument that causes substitution failures for types that cannot be allocators. * testsuite/21_strings/basic_string/cons/char/deduction.cc: New. * testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc: New. From-SVN: r248967 --- libstdc++-v3/ChangeLog | 12 ++ libstdc++-v3/include/bits/alloc_traits.h | 16 +++ libstdc++-v3/include/bits/basic_string.h | 12 ++ libstdc++-v3/include/ext/alloc_traits.h | 2 +- .../basic_string/cons/char/deduction.cc | 118 ++++++++++++++++++ .../basic_string/cons/wchar_t/deduction.cc | 77 ++++++++++++ 6 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/cons/char/deduction.cc create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9abc73cb579..19bab89c8e7 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2017-06-07 Jonathan Wakely + + * include/bits/alloc_traits.h (__is_allocator, _RequireAllocator): + New trait and alias for detecting Allocator-like types. + * include/bits/basic_string.h (basic_string): Add deduction guide + from P0433. + * include/ext/alloc_traits.h (__gnu_cxx::__alloc_traits): Add template + parameter with default template argument that causes substitution + failures for types that cannot be allocators. + * testsuite/21_strings/basic_string/cons/char/deduction.cc: New. + * testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc: New. + 2017-06-02 Jonathan Wakely PR libstdc++/80939 diff --git a/libstdc++-v3/include/bits/alloc_traits.h b/libstdc++-v3/include/bits/alloc_traits.h index 4d1e489b0c7..86a48594ea1 100644 --- a/libstdc++-v3/include/bits/alloc_traits.h +++ b/libstdc++-v3/include/bits/alloc_traits.h @@ -598,6 +598,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : is_copy_constructible<_Tp> { }; +#if __cplusplus >= 201103L + // Trait to detect Allocator-like types. + template + struct __is_allocator : false_type { }; + + template + struct __is_allocator<_Alloc, + __void_t().allocate(size_t{}))>> + : true_type { }; + + template + using _RequireAllocator + = typename enable_if<__is_allocator<_Alloc>::value, _Alloc>::type; +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b6693c440c0..519d686063e 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -5674,6 +5674,18 @@ _GLIBCXX_END_NAMESPACE_CXX11 }; #endif // !_GLIBCXX_USE_CXX11_ABI +#if __cpp_deduction_guides >= 201606 +_GLIBCXX_BEGIN_NAMESPACE_CXX11 + template::value_type, + typename _Allocator = allocator<_CharT>, + typename = _RequireInputIter<_InputIterator>, + typename = _RequireAllocator<_Allocator>> + basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator()) + -> basic_string<_CharT, char_traits<_CharT>, _Allocator>; +_GLIBCXX_END_NAMESPACE_CXX11 +#endif + // operator+ /** * @brief Concatenate two strings. diff --git a/libstdc++-v3/include/ext/alloc_traits.h b/libstdc++-v3/include/ext/alloc_traits.h index f4435ce00a9..fe5655100a4 100644 --- a/libstdc++-v3/include/ext/alloc_traits.h +++ b/libstdc++-v3/include/ext/alloc_traits.h @@ -46,7 +46,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @brief Uniform interface to C++98 and C++11 allocators. * @ingroup allocators */ -template +template struct __alloc_traits #if __cplusplus >= 201103L : std::allocator_traits<_Alloc> diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/deduction.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/deduction.cc new file mode 100644 index 00000000000..c9af333f28e --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/deduction.cc @@ -0,0 +1,118 @@ +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +#include +#include + +template + using input_iterator_seq + = __gnu_test::test_container; + +template struct require_same; +template struct require_same { using type = void; }; + +template + typename require_same::type + check_type(U&) { } + +void +test01() +{ + std::string s0; + std::allocator a; + + std::basic_string s1 = s0; + check_type(s1); + + std::basic_string s2 = std::move(s0); + check_type(s2); + + const std::basic_string s3 = s0; + check_type(s3); + + const std::basic_string s4 = s3; + check_type(s4); + + std::basic_string s5(s0, a); + check_type(s5); + + std::basic_string s6(std::move(s0), a); + check_type(s6); + + std::basic_string s7(s0, 0, 0); + check_type(s7); +} + +void +test02() +{ + char a[1] = {}; + input_iterator_seq seq(a); + + std::basic_string s1(seq.begin(), seq.end()); + check_type(s1); + + std::basic_string s2(seq.begin(), seq.end(), std::allocator()); + check_type(s2); + + std::basic_string s3((char)1, 'a'); + check_type(s3); + + std::basic_string s4((char)1, 'a', std::allocator()); + check_type(s4); +} + +void +test03() +{ + char16_t a[1] = {}; + input_iterator_seq seq(a); + + std::basic_string s1(seq.begin(), seq.end()); + check_type(s1); + + std::basic_string s2(seq.begin(), seq.end(), std::allocator()); + check_type(s2); + + std::basic_string s3((char16_t)1, u'a'); + check_type(s3); + + std::basic_string s4((char16_t)1, u'a', std::allocator()); + check_type(s4); +} + +void +test04() +{ + char32_t a[1] = {}; + input_iterator_seq seq(a); + + std::basic_string s1(seq.begin(), seq.end()); + check_type(s1); + + std::basic_string s2(seq.begin(), seq.end(), std::allocator()); + check_type(s2); + + std::basic_string s3((char32_t)1, U'a'); + check_type(s3); + + std::basic_string s4((char32_t)1, U'a', std::allocator()); + check_type(s4); +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc new file mode 100644 index 00000000000..1f8eadb355a --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc @@ -0,0 +1,77 @@ +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++1z } } + +#include +#include + +template struct require_same; +template struct require_same { using type = void; }; + +template + typename require_same::type + check_type(U&) { } + +void +test01() +{ + std::wstring s0; + std::allocator a; + + std::basic_string s1 = s0; + check_type(s1); + + std::basic_string s2 = std::move(s0); + check_type(s2); + + const std::basic_string s3 = s0; + check_type(s3); + + const std::basic_string s4 = s2; + check_type(s4); + + std::basic_string s5(s0, a); + check_type(s5); + + std::basic_string s6(std::move(s0), a); + check_type(s6); + + std::basic_string s7(s0, 0, 0); + check_type(s7); +} + +void +test02() +{ + using namespace __gnu_test; + wchar_t a[1] = {}; + test_container seq(a); + + std::basic_string s1(seq.begin(), seq.end()); + check_type(s1); + + std::basic_string s2(seq.begin(), seq.end(), std::allocator()); + check_type(s2); + + std::basic_string s3((wchar_t)1, L'a'); + check_type(s3); + + std::basic_string s4((wchar_t)1, L'a', std::allocator()); + check_type(s4); +} -- 2.30.2