This code introduces a versatile homogeneous container, GenType
, implemented using std::variant
. To utilize this implementation, define a typedef GeneralType<Types...>
listing all acceptable types. The GenType
implements various functions designed to be type-agnostic, promoting simplicity and readability in usage.
It is advisable to avoid using this in performance-critical code due to the underlying mechanism, std::variant
, which may not be optimal in such contexts.
The code supports a variety of operators and functions, harnessing the operators of the underlying types:
- Unary Operators
operator Type
: Typecasting to an arbitrary type; Security measures are in place to facilitate type casting.operator!
: Negation operatoroperator*
: Dereference operatoroperator++
: Pre-/Postfix increment operatoroperator--
: Pre-/Postfix decrement operator
- Binary Operators
operator<<
: Streaming Operatoroperator+
: Addition operatoroperator-
: Subtraction operatoroperator*
: Multiplication operatoroperator/
: Division operatoroperator%
: Modulus operatoroperator&
: Bitwise AND operatoroperator&&
: Logical AND operatoroperator^
: Exclusive OR operatoroperator|
: Bitwise inclusive OR operatoroperator||
: Logical inclusive OR operatoroperator<
: Comparison smaller operatoroperator>
: Comparison larger operatoroperator<=
: Comparison smaller equal operatoroperator>=
: Comparison larger equal operatoroperator==
: Comparison equality operatoroperator!=
: Comparison inequality operator
You can build the current version of the code by
g++ main.cpp -std=c++20 -o main
I tested the build with g++ (GCC) 12.3.0
on a linux machine.
Currently, clang can not be supported as some features of C++20 aren't supported by clang in a way they are used here.