Custom integer-like datatype with support for static casts without warnings

I’m writing my own little utility library, and I’ve opted to do something which I don’t really see often (and there might be a reason) – I’m trying to implement a ‘perfect’ wrapper around C++ integer classes (think int, long long, etc.). I’ve managed to do this, and it works just fine, but I keep getting warnings from static cast invocations (C4244).

My implementation:

This is my implementation for a ‘replacement’ for the int type, other types are implemented in the same way, but, for the sake of brevity, I’ll just share the int one. (in my source code, I’ve implemented this using macros).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>struct i32 {
constexpr i32() = default;
constexpr i32(int32_t v) : value(v) {}
constexpr i32(const i32& b) = default;
constexpr i32(i32&& b) noexcept : value(std::exchange(b.value, 0)) {}
~i32() = default;
constexpr auto operator=(const i32& b) -> i32& {
if(this != &b) {
value = b.value;
}
return *this;
}
constexpr auto operator=(i32&& b) noexcept -> i32& {
if(this != &b) {
value = std::exchange(b.value, 0);
}
return *this;
}
constexpr auto operator+=(int32_t x) -> i32& {
value += x;
return *this;
}
constexpr auto operator-=(int32_t x) -> i32& {
value -= x;
return *this;
}
constexpr auto operator*=(int32_t x) -> i32& {
value *= x;
return *this;
}
constexpr auto operator%=(int32_t x) -> i32& {
value %= x;
return *this;
}
constexpr auto operator&=(int32_t x) -> i32& {
value &= x;
return *this;
}
constexpr auto operator|=(int32_t x) -> i32& {
value |= x;
return *this;
}
constexpr auto operator^=(int32_t x) -> i32& {
value ^= x;
return *this;
}
constexpr auto operator<<=(int32_t x) -> i32& {
value <<= x;
return *this;
}
constexpr auto operator>>=(int32_t x) -> i32& {
value >>= x;
return *this;
}
constexpr auto operator/=(int32_t x) -> i32& {
value /= x;
return *this;
}
constexpr auto operator++() -> i32& {
++value;
return *this;
}
constexpr auto operator++(int) -> i32 {
i32 tmp(*this);
++(*this);
return tmp;
}
constexpr auto operator--() -> i32& {
--value;
return *this;
}
constexpr auto operator--(int) -> i32 {
i32 tmp(*this);
--(*this);
return tmp;
}
constexpr auto operator~() const -> i32 {
return i32(~value);
}
constexpr operator int32_t() const {
return value;
}
int32_t value;
};
</code>
<code>struct i32 { constexpr i32() = default; constexpr i32(int32_t v) : value(v) {} constexpr i32(const i32& b) = default; constexpr i32(i32&& b) noexcept : value(std::exchange(b.value, 0)) {} ~i32() = default; constexpr auto operator=(const i32& b) -> i32& { if(this != &b) { value = b.value; } return *this; } constexpr auto operator=(i32&& b) noexcept -> i32& { if(this != &b) { value = std::exchange(b.value, 0); } return *this; } constexpr auto operator+=(int32_t x) -> i32& { value += x; return *this; } constexpr auto operator-=(int32_t x) -> i32& { value -= x; return *this; } constexpr auto operator*=(int32_t x) -> i32& { value *= x; return *this; } constexpr auto operator%=(int32_t x) -> i32& { value %= x; return *this; } constexpr auto operator&=(int32_t x) -> i32& { value &= x; return *this; } constexpr auto operator|=(int32_t x) -> i32& { value |= x; return *this; } constexpr auto operator^=(int32_t x) -> i32& { value ^= x; return *this; } constexpr auto operator<<=(int32_t x) -> i32& { value <<= x; return *this; } constexpr auto operator>>=(int32_t x) -> i32& { value >>= x; return *this; } constexpr auto operator/=(int32_t x) -> i32& { value /= x; return *this; } constexpr auto operator++() -> i32& { ++value; return *this; } constexpr auto operator++(int) -> i32 { i32 tmp(*this); ++(*this); return tmp; } constexpr auto operator--() -> i32& { --value; return *this; } constexpr auto operator--(int) -> i32 { i32 tmp(*this); --(*this); return tmp; } constexpr auto operator~() const -> i32 { return i32(~value); } constexpr operator int32_t() const { return value; } int32_t value; }; </code>
struct i32 {
    constexpr i32() = default;
    constexpr i32(int32_t v) : value(v) {}
    constexpr i32(const i32& b) = default;
    constexpr i32(i32&& b) noexcept : value(std::exchange(b.value, 0)) {}

    ~i32() = default;

    constexpr auto operator=(const i32& b) -> i32& {
        if(this != &b) {
            value = b.value;
        }
        return *this;
    }
    constexpr auto operator=(i32&& b) noexcept -> i32& {
        if(this != &b) {
            value = std::exchange(b.value, 0);
        }
        return *this;
    }

    constexpr auto operator+=(int32_t x) -> i32& {
        value += x;
        return *this;
    }
    constexpr auto operator-=(int32_t x) -> i32& {
        value -= x;
        return *this;
    }
    constexpr auto operator*=(int32_t x) -> i32& {
        value *= x;
        return *this;
    }
    constexpr auto operator%=(int32_t x) -> i32& {
        value %= x;
        return *this;
    }
    constexpr auto operator&=(int32_t x) -> i32& {
        value &= x;
        return *this;
    }
    constexpr auto operator|=(int32_t x) -> i32& {
        value |= x;
        return *this;
    }
    constexpr auto operator^=(int32_t x) -> i32& {
        value ^= x;
        return *this;
    }
    constexpr auto operator<<=(int32_t x) -> i32& {
        value <<= x;
        return *this;
    }
    constexpr auto operator>>=(int32_t x) -> i32& {
        value >>= x;
        return *this;
    }
    constexpr auto operator/=(int32_t x) -> i32& {
        value /= x;
        return *this;
    }

    constexpr auto operator++() -> i32& {
        ++value;
        return *this;
    }
    constexpr auto operator++(int) -> i32 {
        i32 tmp(*this);
        ++(*this);
        return tmp;
    }
    constexpr auto operator--() -> i32& {
        --value;
        return *this;
    }
    constexpr auto operator--(int) -> i32 {
        i32 tmp(*this);
        --(*this);
        return tmp;
    }
    constexpr auto operator~() const -> i32 {
        return i32(~value);
    }

    constexpr operator int32_t() const {
        return value;
    }

    int32_t value;
};

Simple test case

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>u64 a = 10; // u64 is a uint64_t replacement
i32 b = static_cast<i32>(a); // argument: conversion from uint64_t to int32_t, possible loss of data.
std::cout << a << 'n'; // works as expected, outputs '10'
</code>
<code>u64 a = 10; // u64 is a uint64_t replacement i32 b = static_cast<i32>(a); // argument: conversion from uint64_t to int32_t, possible loss of data. std::cout << a << 'n'; // works as expected, outputs '10' </code>
u64 a = 10; // u64 is a uint64_t replacement
i32 b = static_cast<i32>(a); // argument: conversion from uint64_t to int32_t, possible loss of data.

std::cout << a << 'n'; // works as expected, outputs '10'

How can I fix this and prevent the warning from appearing (note: I’m not looking for a band aid solution, but a real fix).

I’m using MSVC with C++20

Why?

I’ve opted to reimplement a section of the standard library, both for the purposes of learning, and to get rid of the compilation overhead. I’ve thought it would be nice to be able to, instead of using std::numeric_limits<x>::max() / whatever, do something like i32::max(), and it kind of devolved from there.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật