https://github.com/ollirelander/custom_variant
https://github.com/ollirelander/custom_variant
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ollirelander/custom_variant
- Owner: ollirelander
- License: mit
- Created: 2023-07-19T18:53:23.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-19T19:06:44.000Z (about 2 years ago)
- Last Synced: 2025-03-26T22:14:35.146Z (6 months ago)
- Language: C++
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Custom Variant C++14 [](https://opensource.org/licenses/MIT)
This library provides a custom variant implementation for C++14 that can hold a flexible set of types. It offers fast performance and ease of use, making it a great choice for scenarios where you need a variant type.
## Installation
This is a header-only library, so simply add `custom_variant.hpp` to your project's include path.
## Usage
The custom variant can be constructed using the `variant_t` class template, which allows you to specify the types it can hold. Here are some examples of the usage of variant:
```c++
#include
#include#include
int main() {
using namespace custom_variant;// Create a variant_t that can hold int, double, char, and std::string
variant_t var;// Set the variant to hold an int
var = 42;// Access the value of the variant as an int
int value_int = var.get();
std::cout << "Variant holds an int: " << value_int << std::endl;// Automatic type conversion - retrieving a float value as a double
var = 3.14f;
double value_double = var.get();
std::cout << "Variant holds a float, retrieved as double: " << value_double << std::endl;// Set the variant to hold a std::string
var = "Hello, variant!";// Access the value of the variant as a std::string
std::string value_string = var.get();
std::cout << "Variant holds a string: " << value_string << std::endl;// Accessing the variant's value using index
var = 10;
int value_at_index = var.get<0>(); // Index 0 corresponds to int
std::cout << "Variant at index 0 (int): " << value_at_index << std::endl;// Using the visit function to apply a visitor to the variant
var = "Hello, visit!";
var.visit([](const auto& value) {
std::cout << "Variant holds: " << value << std::endl;
});// Check the index of the current value in the variant
std::cout << "Current index: " << var.index() << std::endl;return 0;
}
```### Performance
The custom variant implementation is designed for optimal performance. In fact, it has been measured to be 80-90% faster than standard variant implementations available in C++. This speed improvement can be crucial for performance-critical applications.
### Simplicity
Using the custom variant is straightforward, and it provides a convenient and intuitive interface for manipulating values of different types. The `variant_t` class template handles type conversions automatically, making it fast and simple to use in various scenarios.
Whether you need to store different data types in a single container, handle variant inputs, or create flexible data structures, the custom variant offers a powerful and efficient solution.
Give it a try and experience the speed and simplicity of the custom variant for your C++ projects!