实现:

// 使用 static 特性进行常量存储
template<typename _Tp, _Tp __v>
struct integral_constant
{
    static constexpr _Tp value = __v;
    using value_type           =         _Tp;
    using type                 =         integral_constant<_Tp, __v>;
    constexpr operator value_type() const noexcept { return value; }
    constexpr value_type operator()() const noexcept { return value; }
};

template<typename _Tp, _Tp __v>
constexpr _Tp integral_constant<_Tp, __v>::value;

// 重要存储 bool 常量
//type_traits 中等同于 bool 类型
using true_type = integral_constant<bool, true>;
using false_type = integral_constant<bool, false>;

template<bool B>
using bool_constant = integral_constant<bool, B>;

测试程序:

class test{};

int main(){
		std::cout << "integral_constant test: " << std::endl;
    bool test1;
    mySTL::true_type t_type;
    test1 = t_type;
    std::cout << test1 << std::endl;
    mySTL::false_type f_type;
    test1 = f_type;
    std::cout << test1 << std::endl;
    bool test2 = t_type();
    std::cout << test2 << std::endl;
    test2 = f_type();
    std::cout << test2 << std::endl;

    mySTL::integral_constant<int, 5> intConstant;
    int a = intConstant();
    int b = intConstant;
    std::cout << a << " " << b << std::endl;
    
		return 0;
}