make_unique.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_COMMON_MAKE_UNIQUE_H_
18 #define CARTOGRAPHER_COMMON_MAKE_UNIQUE_H_
19 
20 #include <cstddef>
21 #include <memory>
22 #include <type_traits>
23 #include <utility>
24 
25 namespace cartographer {
26 namespace common {
27 
28 // Implementation of c++14's std::make_unique, taken from
29 // https://isocpp.org/files/papers/N3656.txt
30 template <class T>
31 struct _Unique_if {
32  typedef std::unique_ptr<T> _Single_object;
33 };
34 
35 template <class T>
36 struct _Unique_if<T[]> {
37  typedef std::unique_ptr<T[]> _Unknown_bound;
38 };
39 
40 template <class T, size_t N>
41 struct _Unique_if<T[N]> {
42  typedef void _Known_bound;
43 };
44 
45 template <class T, class... Args>
46 typename _Unique_if<T>::_Single_object make_unique(Args&&... args) {
47  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
48 }
49 
50 template <class T>
52  typedef typename std::remove_extent<T>::type U;
53  return std::unique_ptr<T>(new U[n]());
54 }
55 
56 template <class T, class... Args>
57 typename _Unique_if<T>::_Known_bound make_unique(Args&&...) = delete;
58 
59 } // namespace common
60 } // namespace cartographer
61 
62 #endif // CARTOGRAPHER_COMMON_MAKE_UNIQUE_H_
std::unique_ptr< T[]> _Unknown_bound
Definition: make_unique.h:37
_Unique_if< T >::_Single_object make_unique(Args &&...args)
Definition: make_unique.h:46
std::unique_ptr< T > _Single_object
Definition: make_unique.h:32


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:39