aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/explicit_type.h
blob: 64dddd939aa6fd38d5b33e7252d458a945138092 (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, 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_;
};