Skip to content

provides std::to_integer<>() and std::numeric_cast<>(), std::to_enum<>() with runtime overflow check

License

Notifications You must be signed in to change notification settings

qingfengxia/cpp_numeric_cast

Repository files navigation

Travis Ubuntu Build Status Build Status

codecov

![Boost Software License][license-badge]

Development and demonstration of some cpp proposals

This repository holds code demonstration related safe numerics

Download the header-only library and use it in your project

Boost Software License. It is free for open source and close source project.

Proposals

Runtime safe numeric_cast

usage:

long l = INT_MAX + (long)1;
int i = numeric_cast<>(1);  // this will throw overflow exception!
auto b = is_convertable<int>(l);  // gives false, let you test overflow without using C++ exception

is_numeric<T>::value  // can be used as in template, inside std::enable_if<>

proposal: numeric_cast

Reuse keyword explicit to prevent implicit conversion of function parameter

proposal: Reuse keyword explicit to prevent implicit conversion of function parameter

Misc

Demonstration of has_member() type trait

It has already introduced in C++20, here there is a demo to use them has_member.h, and also collected solution from Stackoverflow, to implement in C++03, C++11.

About

provides std::to_integer<>() and std::numeric_cast<>(), std::to_enum<>() with runtime overflow check

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published