std::basic_string に対する推定ガイド
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <string> で定義
|
||
template<class InputIt, class Alloc = std::allocator< typename std::iterator_traits<InputIt>::value_type>> basic_string(InputIt, InputIt, Alloc = Alloc()) -> basic_string<typename std::iterator_traits<InputIt>::value_type, std::char_traits<typename std::iterator_traits<InputIt>::value_type>, Alloc>; |
(1) | (C++17以上) |
template<class CharT, class Traits, class Alloc = std::allocator<CharT>> explicit basic_string(std::basic_string_view<CharT, Traits>, const Alloc& = Alloc()) -> basic_string<CharT, Traits, Alloc>; |
(2) | (C++17以上) |
template<class CharT, class Traits, class Alloc = std::allocator<CharT>>> basic_string(std::basic_string_view<CharT, Traits>, typename /*see below*/::size_type, typename /*see below*/::size_type, const Alloc& = Alloc()) -> basic_string<CharT, Traits, Alloc>; |
(3) | (C++17以上) |
1) イテレータ範囲からの推定を可能とするため、この推定ガイドが std::basic_string に対して提供されます。 このオーバーロードは、
InputIt が LegacyInputIterator を満たし、 Alloc が Allocator を満たす場合にのみ、オーバーロード解決に参加します。2-3) std::basic_string_view からの推定を可能とするため、これらの推定ガイドが std::basic_string に対して提供されます。 (3) の
size_type 引数は推定ガイドによって推定された型の size_type メンバ型を参照します。 これらのオーバーロードは、Alloc が Allocator を満たす場合にのみ、オーバーロード解決に参加します。ノート: ある型が LegacyInputIterator を満たさないとライブラリが判断する範囲は、少なくとも整数型が入力イテレータとして適合しないことを除いて、未規定です。 同様に、ある型が Allocator を満たさないと判断される範囲も、少なくともメンバ型 Alloc::value_type が存在しなければならず、式 std::declval<Alloc&>().allocate(std::size_t{}) が評価されない被演算子として扱われたときに well-formed でなければならないことを除いて、未規定です。
ノート
ガイド (2-3) は、 std::basic_string_view のための std::basic_string のコンストラクタが既存のコードに曖昧性が生じるのを回避するためにテンプレート化され、それらのテンプレートがクラステンプレート引数推定をサポートしないために、必要です。
欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
| DR | 適用先 | 発行時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3075 | C++17 | deduction from basic_string_view was unsupported (exacerbated by LWG issue 2946)
|
deduction guides added |
例
Run this code
#include <string>
#include <vector>
int main() {
std::vector<char> v = {'a', 'b', 'c'};
std::basic_string s(v.begin(), v.end()); // uses explicit deduction guide
}