Skip to content

Safely convert between arithmetic types

License

Notifications You must be signed in to change notification settings

denniskb/narrow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

narrow()

Safely convert between various arithmetic types:

int   x = narrow<int>(1234567890llu); // ok
int   x = narrow<int>(12345.0);       // ok
float x = narrow<float>(-16'000'000); // ok

char  x = narrow<char>(128);          // throws
int   x = narrow<int>(3.14f);         // throws
float x = narrow<float>(0.1);         // throws

narrow<To>(From x) can be used to safely convert the value x from its source type From to the destination type To. If x can be preciesly represented as a To, static_cast<To>(x) is returned, otherwise the function throws.

About

Safely convert between arithmetic types

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages