Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
move_into_vector.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#pragma once
5
6#include "../../namespaces.h"
7
8#include <vector>
9#include <tuple>
10
12
14
15 /*!
16 * Returns the type of the first element of Args.
17 *
18 * Args is expected to have at least one
19 */
20 template<typename... Args>
21 using first_from_pack_t = std::tuple_element_t<0, std::tuple<Args...>>;
22
23 } // end QDOC_CATCH_GENERATORS_TRAITS_NAMESPACE
24
25
26 /*!
27 * Builds an std::vector by moving \a movables into it.
28 *
29 * \a movables must be made of homogenous types.
30 *
31 * This function is intended to allow the construction of an
32 * std::vector<T>, where T is a move only type, as an expression,
33 * to lighten the idiom.
34 *
35 * For example, Catch's GeneratorWrapper<T> adapts a
36 * std::unique_ptr, which is move only, making it impossible to
37 * build a std::vector from them in place.
38 *
39 * Then, everywhere this is needed, a more complex approach of
40 * generating the collection of objects, generating a vector of a
41 * suitable size and iterating the objects to move-emplace them in
42 * the vector is required.
43 *
44 * This not only complicates the code but is incompatible with a
45 * GENERATE expression, making it extremely hard, noisy and error
46 * prone to use them together.
47 *
48 * In those cases, then, a call to move_into_vector can be used as
49 * an expression to circumvent the problem.
50 */
51 template<typename... MoveOnlyTypes>
52 inline auto move_into_vector(MoveOnlyTypes... movables) {
53 std::vector<QDOC_CATCH_GENERATORS_TRAITS_NAMESPACE::first_from_pack_t<MoveOnlyTypes...>>
54 moved_into_vector;
55 moved_into_vector.reserve(sizeof...(movables));
56
57 (moved_into_vector.emplace_back(std::move(movables)), ...);
58
59 return moved_into_vector;
60 }
61
62} // end QDOC_CATCH_GENERATORS_UTILITIES_ABSOLUTE_NAMESPACE
auto move_into_vector(MoveOnlyTypes... movables)
Builds an std::vector by moving movables into it.
#define QDOC_CATCH_GENERATORS_UTILITIES_ABSOLUTE_NAMESPACE
Definition namespaces.h:14
#define QDOC_CATCH_GENERATORS_TRAITS_NAMESPACE
Definition namespaces.h:10