This project implements a dynamic typing system in C++, allowing variables of different data types to interact and perform mathematical operations at runtime. Inspired by dynamically typed languages like Python, this project leverages advanced Object-Oriented Programming (OOP) principles to resolve cross-type operations safely and efficiently.
Through operator overloading and a smart wrapper class, the library provides a high-level, Python-like syntax while maintaining native C++ performance and strict memory safety.
The core of the mathematical engine relies on the Double Dispatch design pattern. To perform operations between two unknown dynamic types, the project implements a two-step resolution mechanism:
-
First Dispatch: The operation is called on the first object, passing the second object as an argument (e.g.,
ObjectA->add(ObjectB)). -
Second Dispatch: The first object calls a reverse operation on the second object, passing itself as a specific type (e.g.,
ObjectB->radd(this)).
This eliminates the need for expensive and unsafe type-checking (dynamic_cast), delegating the type resolution directly to the objects' virtual tables (V-Tables).
To hide raw pointers and manage memory automatically, the system uses a polymorphic wrapper class (Type) backed by a std::pmr::unsynchronized_pool_resource instead of standard new allocations.
This pool-based architecture prevents memory leaks, avoids heap fragmentation, and eliminates the need for manual delete operations.
-
Python-like Syntax: Seamless operations using standard C++ operators (
+,-,*,/,==,<,>,<=,>=) via operator overloading. -
Automatic Memory Management: Zero memory leaks and no manual pointer management thanks to
std::pmr::unsynchronized_pool_resource. -
Dynamic Resolution: Cross-type mathematical operations (e.g., Int + Float returns Float, String * Int returns repeated String).
-
Type Conversion & I/O: Built-in functions (
boolean(),str(),intT(),floatT(),print(),input()) for type casting and standard I/O operations. -
Type Introspection: Built-in Python-like functions such as
isinstance()andtype()to inspect dynamic metadata at runtime (all functions infunctions.hreturn aType). -
Type Safety & Exceptions: Invalid operations (e.g., String + Float) throw explicit standard C++ exceptions (
std::logic_error,std::overflow_error).
-
TypeAll.h/pointer_wrapper.h: Contains theTypewrapper class managing memory and operator overloading. -
dtypes.h: Contains the abstract base classObjectand the forward declarations. -
intObj.h/floatObj.h/stringObj.h/boolObj.h: Implementations of the specific data types and dispatch logic. -
functions.h: Contains high-level helper functions likeisinstance(),type(),boolean(),str(),intT(),floatT(),print(), andinput()(all returning aType). -
basic_operations.h: Contains pure functional logic decoupled from objects.
#include <iostream>
#include "TypeAll.h"
#include "functions.h"
int main() {
// 1. Variable initialization
Type varInt = standardMemPool.push(IntObj(10));
Type varFloat = standardMemPool.push(FloatObj(2.5));
Type varStr = standardMemPool.push(StringObj("C++ "));
// 2. Dynamic Mathematics
Type sum = varInt + varFloat;
std::cout << "10 + 2.5 = " << sum.getFloat() << std::endl; // Output: 12.5
// 3. String Operations
Type multipliedText = varStr * standardMemPool.push(FloatObj(3.0));
std::cout << "'C++ ' * 3.0 = " << multipliedText.getString() << std::endl; // Output: C++ C++ C++
// 4. Metadata and Introspection
if (isinstance(varStr, "string")) {
std::cout << "varStr is dynamically recognized as a string!" << std::endl;
}
std::cout << "Type of sum: " << type(sum) << std::endl; // Output: float
// 5. Automatic Memory Cleanup
}According to the current tracking configuration:
-
Int: Support for add, sub, mul, div.
-
Float: Support for add, sub, mul, div.
-
String: Full implementation (including multiplication by non-decimal floats).
-
Memory Management: Migrated to
std::pmr::unsynchronized_pool_resource(eliminatingnewand preventing memory leaks). -
Bool: Total support added.
-
Operations: Support added for
==,<,>,<=,>=. -
Type Conversions & I/O: Added
boolean,str,intT,floatT,print, andinputreturning aType.