81 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310 11// <unordered_set>12 13// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,14// class Alloc = allocator<Value>>15// class unordered_set16 17// template <class... Args>18// pair<iterator, bool> emplace(Args&&... args);19 20#include <cassert>21#include <unordered_set>22 23#include "../../Emplaceable.h"24#include "MoveOnly.h"25#include "min_allocator.h"26 27int main(int, char**) {28 {29 typedef std::unordered_set<Emplaceable> C;30 typedef std::pair<C::iterator, bool> R;31 C c;32 R r = c.emplace();33 assert(c.size() == 1);34 assert(*r.first == Emplaceable());35 assert(r.second);36 37 r = c.emplace(Emplaceable(5, 6));38 assert(c.size() == 2);39 assert(*r.first == Emplaceable(5, 6));40 assert(r.second);41 42 r = c.emplace(5, 6);43 assert(c.size() == 2);44 assert(*r.first == Emplaceable(5, 6));45 assert(!r.second);46 }47 {48 typedef std::49 unordered_set<Emplaceable, std::hash<Emplaceable>, std::equal_to<Emplaceable>, min_allocator<Emplaceable>>50 C;51 typedef std::pair<C::iterator, bool> R;52 C c;53 R r = c.emplace();54 assert(c.size() == 1);55 assert(*r.first == Emplaceable());56 assert(r.second);57 58 r = c.emplace(Emplaceable(5, 6));59 assert(c.size() == 2);60 assert(*r.first == Emplaceable(5, 6));61 assert(r.second);62 63 r = c.emplace(5, 6);64 assert(c.size() == 2);65 assert(*r.first == Emplaceable(5, 6));66 assert(!r.second);67 }68 { // We're unwrapping pairs for `unordered_{,multi}map`. Make sure we're not trying to do that for unordered_set.69 struct PairHasher {70 size_t operator()(const std::pair<MoveOnly, MoveOnly>& val) const { return std::hash<MoveOnly>()(val.first); }71 };72 using Set = std::unordered_set<std::pair<MoveOnly, MoveOnly>, PairHasher>;73 Set set;74 auto res = set.emplace(std::pair<MoveOnly, MoveOnly>(2, 4));75 assert(std::get<1>(res));76 assert(set.begin() == std::get<0>(res));77 }78 79 return 0;80}81