2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <type_traits>
23 /// In functional programming, the degenerate case is often called "unit". In
24 /// C++, "void" is often the best analogue. However, because of the syntactic
25 /// special-casing required for void, it is frequently a liability for template
26 /// metaprogramming. So, instead of writing specializations to handle cases like
27 /// SomeContainer<void>, a library author may instead rule that out and simply
28 /// have library users use SomeContainer<Unit>. Contained values may be ignored.
31 /// "void" is the type that admits of no values at all. It is not possible to
32 /// construct a value of this type.
33 /// "unit" is the type that admits of precisely one unique value. It is
34 /// possible to construct a value of this type, but it is always the same value
35 /// every time, so it is uninteresting.
37 // These are structs rather than type aliases because MSVC 2017 RC has
38 // trouble correctly resolving dependent expressions in type aliases
39 // in certain very specific contexts, including a couple where this is
40 // used. See the known issues section here for more info:
41 // https://blogs.msdn.microsoft.com/vcblog/2016/06/07/expression-sfinae-improvements-in-vs-2015-update-3/
44 struct Lift : std::conditional<std::is_same<T, void>::value, Unit, T> {};
46 using LiftT = typename Lift<T>::type;
48 struct Drop : std::conditional<std::is_same<T, Unit>::value, void, T> {};
50 using DropT = typename Drop<T>::type;
52 constexpr bool operator==(const Unit& /*other*/) const {
55 constexpr bool operator!=(const Unit& /*other*/) const {
60 constexpr Unit unit {};