aboutsummaryrefslogtreecommitdiff
path: root/lib/function_traits.cpp
blob: e3d271225ec08251c809bc4db07d8183df22f78b (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
// SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net>
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "function_traits.h"

// Tests for function_traits<>

using namespace Quotient;

template <typename FnT>
using fn_return_t = typename function_traits<FnT>::return_type;

int f_();
static_assert(std::is_same_v<fn_return_t<decltype(f_)>, int>,
              "Test fn_return_t<>");

void f1_(int, float);
static_assert(std::is_same_v<fn_arg_t<decltype(f1_), 1>, float>,
              "Test fn_arg_t<>");

struct Fo {
    int operator()();
    static constexpr auto l = [] { return 0.0f; };
    bool memFn();
    void constMemFn() const&;
    double field;
    const double field2;
};
static_assert(std::is_same_v<fn_return_t<Fo>, int>,
              "Test return type of function object");
static_assert(std::is_same_v<fn_return_t<decltype(Fo::l)>, float>,
              "Test return type of lambda");
static_assert(std::is_same_v<fn_arg_t<decltype(&Fo::memFn)>, Fo>,
              "Test first argument type of member function");
static_assert(std::is_same_v<fn_return_t<decltype(&Fo::memFn)>, bool>,
              "Test return type of member function");
static_assert(std::is_same_v<fn_arg_t<decltype(&Fo::constMemFn)>, const Fo&>,
              "Test first argument type of const member function");
static_assert(std::is_void_v<fn_return_t<decltype(&Fo::constMemFn)>>,
              "Test return type of const member function");
static_assert(std::is_same_v<fn_return_t<decltype(&Fo::field)>, double&>,
              "Test return type of a class member");
static_assert(std::is_same_v<fn_return_t<decltype(&Fo::field2)>, const double&>,
              "Test return type of a const class member");

struct Fo1 {
    void operator()(int);
};
static_assert(std::is_same_v<fn_arg_t<Fo1>, int>,
              "Test fn_arg_t defaulting to first argument");

template <typename T>
[[maybe_unused]] static void ft(const std::vector<T>&);
static_assert(
    std::is_same<fn_arg_t<decltype(ft<double>)>, const std::vector<double>&>(),
    "Test function templates");