You could start writing a wrapper for your integral types to match the exact type (or some conditions).
#include <cstdint>
#include <iostream>
#include <limits>
#include <type_traits>
#include <vector>
template <class...> struct conjunction : std::true_type {};
template <class B1> struct conjunction<B1> : B1 {};
template <class B1, class... Bn>
struct conjunction<B1, Bn...>
: std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
template <typename T> struct int_wrapper {
explicit int_wrapper() : _val{T{}} {}
explicit int_wrapper(const int_wrapper &other) : _val{other._val} {}
template <typename U> explicit int_wrapper(U val) : _val{val} {
static_assert(sizeof(T) >= sizeof(U), "Size mismatch.");
static_assert(conjunction<std::is_signed<T>, std::is_signed<U>>::value,
"sign mismatch");
}
explicit operator T() { return _val; }
explicit operator T() const { return _val; }
T _val;
};
std::ostream &operator<<(std::ostream &stream, const int_wrapper<int16_t> &v) {
stream << v._val;
return stream;
}
int main() {
std::vector<int_wrapper<int16_t>> v;
v.emplace_back(std::numeric_limits<uint64_t>::max());
std::cout << v.back() << std::endl;
return 0;
}
clang on macOS gives you error like:
so.cpp:18:60: error: non-constant-expression cannot be narrowed from type 'unsigned long long' to 'short' in initializer list
[-Wc++11-narrowing]
template <typename U> explicit int_wrapper(U val) : _val{val} {
^~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1752:31: note: in
instantiation of function template specialization 'int_wrapper<short>::int_wrapper<unsigned long long>' requested here
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1668:18: note: in
instantiation of function template specialization 'std::__1::allocator<int_wrapper<short> >::construct<int_wrapper<short>,
unsigned long long>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1514:14: note: in
instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<int_wrapper<short> >
>::__construct<int_wrapper<short>, unsigned long long>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1643:25: note: in
instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<int_wrapper<short> >
>::construct<int_wrapper<short>, unsigned long long>' requested here
__alloc_traits::construct(this->__alloc(),
^
so.cpp:37:5: note: in instantiation of function template specialization 'std::__1::vector<int_wrapper<short>,
std::__1::allocator<int_wrapper<short> > >::emplace_back<unsigned long long>' requested here
v.emplace_back(std::numeric_limits<uint64_t>::max());
^
so.cpp:18:60: note: insert an explicit cast to silence this issue
template <typename U> explicit int_wrapper(U val) : _val{val} {
^~~
static_cast<short>( )
so.cpp:19:5: error: static_assert failed "Not the same size."
static_assert(sizeof(T) >= sizeof(U), "Not the same size.");
^ ~~~~~~~~~~~~~~~~~~~~~~
so.cpp:20:5: error: static_assert failed "sign mismatch"
static_assert(conjunction<std::is_signed<T>, std::is_signed<U>>::value,
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 errors generated.