aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/explicit_type.h
blob: 41f85f52a0c5c6bdc6b0748a7226234959d94730 (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
#pragma once

#include "typetraits.h"

/**
 * Helper type that can be used as one of the parameters in function declaration
 * to limit the number of types this function can be called with.
 *
 * Example usage:
 * @code
 * void CharOnlyFunction(TExplicitType<char> value);
 * void AnythingFunction(char value);
 *
 * CharOnlyFunction('c'); // Works.
 * CharOnlyFunction(1); // Compilation error.
 * CharOnlyFunction(1ull); // Compilation error.
 *
 * AnythingFunction('c'); // Works.
 * AnythingFunction(1); // Works.
 * AnythingFunction(1ull); // Works.
 * @endcode
 */
template <class T>
class TExplicitType {
public:
    template <class OtherT>
    TExplicitType(const OtherT& value Y_LIFETIME_BOUND, std::enable_if_t<std::is_same<OtherT, T>::value>* = nullptr) noexcept
        : Value_(value)
    {
    }

    const T& Value() const noexcept {
        return Value_;
    }

    operator const T&() const noexcept {
        return Value_;
    }

private:
    const T& Value_;
};