GNSS-SDR  0.0.13
An Open Source GNSS Software Defined Receiver
gnss_sdr_make_unique.h
Go to the documentation of this file.
1 /*!
2  * \file gnss_sdr_make_unique.h
3  * \brief This file implements std::make_unique for C++11
4  *
5  * \author Carles Fernandez-Prades, 2020. cfernandez(at)cttc.es
6  *
7  * Based on https://stackoverflow.com/a/17902439
8  *
9  *
10  * -----------------------------------------------------------------------------
11  *
12  * Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
13  *
14  * GNSS-SDR is a software defined Global Navigation
15  * Satellite Systems receiver
16  *
17  * This file is part of GNSS-SDR.
18  *
19  * SPDX-License-Identifier: GPL-3.0-or-later
20  *
21  * -----------------------------------------------------------------------------
22  */
23 
24 #ifndef GNSS_SDR_GNSS_SDR_MAKE_UNIQUE_H
25 #define GNSS_SDR_GNSS_SDR_MAKE_UNIQUE_H
26 
27 #if __cplusplus == 201103L
28 
29 #include <cstddef>
30 #include <memory>
31 #include <type_traits>
32 #include <utility>
33 
34 namespace std
35 {
36 template <class T>
37 struct _Unique_if
38 {
39  typedef unique_ptr<T> _Single_object;
40 };
41 
42 template <class T>
43 struct _Unique_if<T[]>
44 {
45  typedef unique_ptr<T[]> _Unknown_bound;
46 };
47 
48 template <class T, size_t N>
49 struct _Unique_if<T[N]>
50 {
51  typedef void _Known_bound;
52 };
53 
54 template <class T, class... Args>
55 typename _Unique_if<T>::_Single_object
56 make_unique(Args&&... args)
57 {
58  return unique_ptr<T>(new T(std::forward<Args>(args)...));
59 }
60 
61 template <class T>
62 typename _Unique_if<T>::_Unknown_bound
63 make_unique(size_t n)
64 {
65  typedef typename remove_extent<T>::type U;
66  return unique_ptr<T>(new U[n]());
67 }
68 
69 template <class T, class... Args>
70 typename _Unique_if<T>::_Known_bound
71 make_unique(Args&&...) = delete;
72 } // namespace std
73 
74 #endif // __cplusplus == 201103L
75 
76 #endif // GNSS_SDR_GNSS_SDR_MAKE_UNIQUE_H
STL namespace.