Safe and Reliable Type Conversions for C++
SafePodCasting is a lightweight 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, overflows, underflows, and undefined behavior.
Whether you're working with signed and unsigned integers, floating-point numbers, or boolean values, SafePodCasting provides comprehensive coverage and error prevention mechanisms to make your code more robust.
Convert between different primitive types with built-in safety checks to prevent data loss.
Supports all common primitive types including bool, int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, and double.
Prevents overflows, underflows, and other issues that can arise during unsafe type casting.
Implemented as a single header and source file, making it easy to integrate into any project.
A C++ compiler that supports C++11 or later.
git clone https://github.com/nguyenchiemminhvu/SafePodCasting.git
The library provides a safe_cast class with static methods for type conversions. You can also use the SAFE_CAST template function for a more concise syntax.
#include "safecast.h"
#include <iostream>
int main()
{
int32_t value = 42;
int8_t int8Value = safe_cast::int32_to_int8(value);
float floatValue = safe_cast::int32_to_float(value);
double doubleValue = safe_cast::int32_to_double(value);
std::cout << "int8_t: " << static_cast<int>(int8Value) << "\n";
std::cout << "float: " << floatValue << "\n";
std::cout << "double: " << doubleValue << "\n";
return 0;
}
#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;
}
g++ -std=c++11 -o example main.cpp safecast.cpp
Get the latest version of SafePodCasting from GitHub
View on GitHub Download ZIPContributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
MIT License
Copyright (c) 2025 nguyenchiemminhvu@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.