5f6e5d4aa4d407324a52821bfa2ba771669bc879
[folly.git] / folly / ContainerTraits.h
1 /*
2  * Copyright 2015 Facebook, Inc.
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 FOLLY_BASE_CONTAINER_TRAITS_H_
18 #define FOLLY_BASE_CONTAINER_TRAITS_H_
19
20 #include <folly/Traits.h>
21
22 namespace folly {
23
24 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(container_emplace_back_traits, emplace_back);
25
26 template <class Container, typename... Args>
27 inline
28 typename std::enable_if<
29     container_emplace_back_traits<Container, void(Args...)>::value>::type
30 container_emplace_back_or_push_back(Container& container, Args&&... args) {
31   container.emplace_back(std::forward<Args>(args)...);
32 }
33
34 template <class Container, typename... Args>
35 inline
36 typename std::enable_if<
37     !container_emplace_back_traits<Container, void(Args...)>::value>::type
38 container_emplace_back_or_push_back(Container& container, Args&&... args) {
39   using v = typename Container::value_type;
40   container.push_back(v(std::forward<Args>(args)...));
41 }
42
43 }
44
45 #endif