aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/concepts.h
blob: 976c707ffec8cc015f7a248687471e9400d082e0 (plain) (blame)
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
#pragma once

#include <concepts>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

template <class T, class TSignature>
struct TIsInvocable;

template <class T, class TResult, bool NoExcept, class... TArgs>
struct TIsInvocable<T, TResult(TArgs...) noexcept(NoExcept)>
{
private:
    static constexpr bool IsInvocable_ = requires (T&& t, TArgs&&... args) {
        { std::forward<T>(t)(std::forward<TArgs>(args)...) } -> std::same_as<TResult>;
    };

    static constexpr bool IsNoThrowInvocable_ = requires (T&& t, TArgs&&... args) {
        { std::forward<T>(t)(std::forward<TArgs>(args)...) } noexcept -> std::same_as<TResult>;
    };

public:
    static constexpr bool Value =
        IsInvocable_ &&
        (!NoExcept || IsNoThrowInvocable_);
};

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

template <class TObject, class TScalar>
concept CScalable = requires (TObject object, TScalar scalar)
{
    { object * scalar } -> std::same_as<TObject>;
};

////////////////////////////////////////////////////////////////////////////////

template <class T, class TSignature>
concept CInvocable = NDetail::TIsInvocable<T, TSignature>::Value;

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT