-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcomplete_invoke.h++
56 lines (48 loc) · 2 KB
/
complete_invoke.h++
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
// Nonius - C++ benchmarking tool
//
// Written in 2014- by the nonius contributors <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>
// Invoke with a special case for void
#ifndef NONIUS_DETAIL_COMPLETE_INVOKE_HPP
#define NONIUS_DETAIL_COMPLETE_INVOKE_HPP
#include <type_traits>
#include <utility>
namespace nonius {
namespace detail {
template <typename T>
struct complete_type { using type = T; };
template <>
struct complete_type<void> { struct type {}; };
template <typename T>
using CompleteType = typename complete_type<T>::type;
template <typename Result>
struct complete_invoker {
template <typename Fun, typename... Args>
static Result invoke(Fun&& fun, Args&&... args) {
return std::forward<Fun>(fun)(std::forward<Args>(args)...);
}
};
template <>
struct complete_invoker<void> {
template <typename Fun, typename... Args>
static CompleteType<void> invoke(Fun&& fun, Args&&... args) {
std::forward<Fun>(fun)(std::forward<Args>(args)...);
return {};
}
};
template <typename Sig>
using ResultOf = typename std::result_of<Sig>::type;
// invoke and not return void :(
template <typename Fun, typename... Args>
CompleteType<ResultOf<Fun(Args...)>> complete_invoke(Fun&& fun, Args&&... args) {
return complete_invoker<ResultOf<Fun(Args...)>>::invoke(std::forward<Fun>(fun), std::forward<Args>(args)...);
}
} // namespace detail
} // namespace nonius
#endif // NONIUS_DETAIL_COMPLETE_INVOKE_HPP