SafePodCasting Code
Brought to you by:
ncmv92
| File | Date | Author | Commit |
|---|---|---|---|
| test | 2025-04-04 |
|
[448e11] release version 1.0 |
| LICENSE | 2025-04-03 |
|
[e71119] Initial commit |
| README.md | 2025-04-04 |
|
[448e11] release version 1.0 |
| safecast.cpp | 2025-04-04 |
|
[448e11] release version 1.0 |
| safecast.h | 2025-04-04 |
|
[448e11] release version 1.0 |
SafePodCasting is a C++ library designed to provide safe and reliable type conversions between various primitive data types. It ensures that conversions are performed with proper bounds checking to prevent data loss or undefined behavior.
bool, int8_t, uint8_t, float, double, etc.) with built-in safety checks.C++ compiler that supports C++11 or later.safecast.h and safecast.cpp files into your project.safecast.h header in your source files where you need safe type conversions.The library provides a SafeCast class with static methods for type conversions. Below is an example of how to use it:
#include "safecast.h"
#include <iostream>
int main()
{
int value = 42;
int8_t int8Value = SafeCast::intToInt8(value);
float floatValue = SafeCast::intToFloat(value);
double doubleValue = SafeCast::intToDouble(value);
std::cout << "int8_t: " << static_cast<int>(int8Value) << "\n";
std::cout << "float: " << floatValue << "\n";
std::cout << "double: " << doubleValue << "\n";
return 0;
}
The library also provides a SAFE_CAST template function for a more concise syntax:
#include "safecast.h"
#include <iostream>
int main()
{
int16_t smallValue = 12345;
// Convert int16_t to int32_t
int32_t int32Value = SAFE_CAST<int16_t, int32_t>(smallValue);
// Convert int16_t to double
double doubleValue = SAFE_CAST<int16_t, double>(smallValue);
// Convert double to int32_t
double largeValue = 12345.6789;
int32_t truncatedValue = SAFE_CAST<double, int32_t>(largeValue);
std::cout << "int32_t: " << int32Value << "\n";
std::cout << "double: " << doubleValue << "\n";
std::cout << "truncated int32_t: " << truncatedValue << "\n";
return 0;
}
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.