IoTeX client
vector.h
1// Copyright (c) 2019 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 <vector>
11
19template<typename... Args>
20inline std::vector<typename std::common_type<Args...>::type> Vector(Args&&... args)
21{
22 std::vector<typename std::common_type<Args...>::type> ret;
23 ret.reserve(sizeof...(args));
24 // The line below uses the trick from
25 // 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
31template<typename V>
32inline V Cat(V v1, V&& v2)
33{
34 v1.reserve(v1.size() + v2.size());
35 for(auto& arg: v2)
36 {
37 v1.push_back(std::move(arg));
38 }
39 return v1;
40}
41
43template<typename V>
44inline V Cat(V v1, const V& v2)
45{
46 v1.reserve(v1.size() + v2.size());
47 for(const auto& arg: v2)
48 {
49 v1.push_back(arg);
50 }
51 return v1;
52}
53
54#endif // BITCOIN_UTIL_VECTOR_H