1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
11// <flat_map>
12
13// mapped_type& operator[](key_type&& k);
14
15#include <flat_map>
16#include <deque>
17#include <functional>
18#include <cassert>
19
20#include "MinSequenceContainer.h"
21#include "../helpers.h"
22#include "test_macros.h"
23#include "MoveOnly.h"
24#include "min_allocator.h"
25
26// Constraints: is_constructible_v<mapped_type> is true.
27template <class M, class Input>
28concept CanIndex = requires(M m, Input k) { m[k]; };
29
30static_assert(CanIndex<std::flat_map<int, double>, int&&>);
31static_assert(!CanIndex<std::flat_map<int, NoDefaultCtr>, int&&>);
32
33template <class KeyContainer, class ValueContainer>
34void test() {
35 {
36 std::flat_map<MoveOnly, double, std::less<MoveOnly>, KeyContainer, ValueContainer> m;
37 ASSERT_SAME_TYPE(decltype(m[MoveOnly{}]), double&);
38 assert(m.size() == 0);
39 assert(m[1] == 0.0);
40 assert(m.size() == 1);
41 m[1] = -1.5;
42 assert(m[1] == -1.5);
43 assert(m.size() == 1);
44 assert(m[6] == 0);
45 assert(m.size() == 2);
46 m[6] = 6.5;
47 assert(m[6] == 6.5);
48 assert(m.size() == 2);
49 }
50}
51
52int main(int, char**) {
53 test<std::vector<MoveOnly>, std::vector<double>>();
54 test<std::deque<MoveOnly>, std::vector<double>>();
55 test<MinSequenceContainer<MoveOnly>, MinSequenceContainer<double>>();
56 test<std::vector<MoveOnly, min_allocator<MoveOnly>>, std::vector<double, min_allocator<double>>>();
57
58 {
59 auto index_func = [](auto& m, auto key_arg, auto value_arg) {
60 using FlatMap = std::decay_t<decltype(m)>;
61 typename FlatMap::key_type key = key_arg;
62 const typename FlatMap::mapped_type value = value_arg;
63 m[std::move(key)] = value;
64 };
65 test_emplace_exception_guarantee(emplace_function&: index_func);
66 }
67 return 0;
68}
69

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_rv_key.pass.cpp