c++ 内置类型忘记初始化

#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <random>

#include <fstream>

using namespace std;


template<typename T>
void init(T &t) {
    t = T{};
}

#define def(type, name) \
    type name;          \
    init(name);


// 用于内置非类类型
template<typename T, enable_if_t<!is_class_v<T>> * = nullptr>
struct Def {
    using type = decay_t<T>;
    type i;

    Def() : i{} {}

    Def(const type &other) : i(other) {};

    Def &operator+=(const Def &other) {
        i += other.i;
        return *this;
    }

    Def &operator-=(const Def &other) {
        i -= other.i;
        return *this;
    }

    Def &operator*=(const Def &other) {
        i *= other.i;
        return *this;
    }

    Def &operator/=(const Def &other) {
        i /= other.i;
        return *this;
    }

    operator type() {
        return i;
    }

};

using Int = Def<int>;
using Double = Def<double>;
using Long = Def<long>;
using Short = Def<short>;

int main() {
    // 定义并初始化
    def (int, i);
    def (int, j);
    def(double, b);
    cout << i << j << b << endl;

    // 定义并初始化
    Int ii;
    Double di;
    Short si;
    cout << ii << "\t" << "\t" << si + 1 << endl;

    di = 10;
    di *= 10;
    cout << di << endl;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值