summaryrefslogtreecommitdiff
path: root/boost/type_traits/detail/is_likely_lambda.hpp
blob: 6da8acafce69dafd6394a4e0ec680c2020702c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Copyright 2017 Joaquin M Lopez Munoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * See http://www.boost.org/libs/poly_collection for library home page.
 */

#ifndef BOOST_TT_DETAIL_IS_LIKELY_STATELESS_LAMBDA_HPP
#define BOOST_TT_DETAIL_IS_LIKELY_STATELESS_LAMBDA_HPP

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/type_traits/detail/config.hpp>
#include <boost/type_traits/integral_constant.hpp>

#if defined(BOOST_TT_HAS_ACCURATE_BINARY_OPERATOR_DETECTION)
//
// We don't need or use this, just define a dummy class:
//
namespace boost{ namespace type_traits_detail{

template<typename T>
struct is_likely_stateless_lambda : public false_type {};

}}

#elif !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && !BOOST_WORKAROUND(BOOST_MSVC, < 1900)

#include <boost/type_traits/is_convertible.hpp>
#include <boost/core/enable_if.hpp>

namespace boost{

namespace type_traits_detail{

/* Stateless lambda expressions have one (and only one) call operator and are
 * convertible to a function pointer with the same signature. Non-lambda types
 * could satisfy this too, hence the "likely" qualifier.
 */

template<typename T>
struct has_one_operator_call_helper
{
  template<typename Q> static boost::true_type  test(decltype(&Q::operator())*);
  template<typename>   static boost::false_type test(...);

  using type=decltype(test<T>(nullptr));
};

template<typename T>
using has_one_operator_call=typename has_one_operator_call_helper<T>::type;

template<typename T>
struct equivalent_function_pointer
{
  template<typename Q,typename R,typename... Args>
  static auto helper(R (Q::*)(Args...)const)->R(*)(Args...);
  template<typename Q,typename R,typename... Args>
  static auto helper(R (Q::*)(Args...))->R(*)(Args...);

  using type=decltype(helper(&T::operator()));
};

template<typename T,typename=void>
struct is_likely_stateless_lambda : false_type{};

template<typename T>
struct is_likely_stateless_lambda<
  T,
  typename boost::enable_if_c<has_one_operator_call<T>::value>::type> : 
     boost::is_convertible<T, typename equivalent_function_pointer<T>::type
>{};

} /* namespace type_traits_detail */

} /* namespace boost */

#else
 //
 // Can't implement this:
 //
namespace boost {
   namespace type_traits_detail {

      template<typename T>
      struct is_likely_stateless_lambda : public boost::integral_constant<bool, false> {};
}}

#endif
#endif