• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*=============================================================================
2    Copyright (c) 2012 Paul Fultz II
3    identity.h
4    Distributed under the Boost Software License, Version 1.0. (See accompanying
5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#ifndef BOOST_HOF_GUARD_FUNCTION_IDENTITY_H
9#define BOOST_HOF_GUARD_FUNCTION_IDENTITY_H
10
11/// identity
12/// ========
13///
14/// Description
15/// -----------
16///
17/// The `identity` function is an unary function object that returns whats given to it.
18///
19/// Semantics
20/// ---------
21///
22///     assert(identity(x) == x);
23///
24/// Synopsis
25/// --------
26///
27///     template<class T>
28///     constexpr T identity(T&& x);
29///
30
31#include <utility>
32#include <initializer_list>
33#include <boost/hof/detail/forward.hpp>
34#include <boost/hof/detail/static_const_var.hpp>
35
36namespace boost { namespace hof { namespace identity_detail {
37
38struct identity_base
39{
40    template<class T>
41    constexpr T operator()(T&& x) const
42    noexcept(std::is_reference<T>::value || BOOST_HOF_IS_NOTHROW_MOVE_CONSTRUCTIBLE(T))
43    {
44        return BOOST_HOF_FORWARD(T)(x);
45    }
46
47    template<class T>
48    constexpr std::initializer_list<T>& operator()(std::initializer_list<T>& x) const noexcept
49    {
50        return x;
51    }
52
53    template<class T>
54    constexpr const std::initializer_list<T>& operator()(const std::initializer_list<T>& x) const noexcept
55    {
56        return x;
57    }
58
59    template<class T>
60    constexpr std::initializer_list<T> operator()(std::initializer_list<T>&& x) const noexcept(noexcept(std::initializer_list<T>(std::move(x))))
61    {
62        return BOOST_HOF_FORWARD(std::initializer_list<T>)(x);
63    }
64};
65
66}
67
68BOOST_HOF_DECLARE_STATIC_VAR(identity, identity_detail::identity_base);
69
70}} // namespace boost::hof
71
72#endif
73