Bitcoin Core  24.99.0
P2P Digital Currency
vector.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_UTIL_VECTOR_H
6 #define BITCOIN_UTIL_VECTOR_H
7 
8 #include <initializer_list>
9 #include <type_traits>
10 #include <utility>
11 #include <vector>
12 
20 template<typename... Args>
21 inline std::vector<typename std::common_type<Args...>::type> Vector(Args&&... args)
22 {
23  std::vector<typename std::common_type<Args...>::type> ret;
24  ret.reserve(sizeof...(args));
25  // The line below uses the trick from https://www.experts-exchange.com/articles/32502/None-recursive-variadic-templates-with-std-initializer-list.html
26  (void)std::initializer_list<int>{(ret.emplace_back(std::forward<Args>(args)), 0)...};
27  return ret;
28 }
29 
31 template<typename V>
32 inline V Cat(V v1, V&& v2)
33 {
34  v1.reserve(v1.size() + v2.size());
35  for (auto& arg : v2) {
36  v1.push_back(std::move(arg));
37  }
38  return v1;
39 }
40 
42 template<typename V>
43 inline V Cat(V v1, const V& v2)
44 {
45  v1.reserve(v1.size() + v2.size());
46  for (const auto& arg : v2) {
47  v1.push_back(arg);
48  }
49  return v1;
50 }
51 
52 #endif // BITCOIN_UTIL_VECTOR_H
int ret
ArgsManager args
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:21
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:32