Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: variant2 #51

Merged
merged 12 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-conan-branch-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
with:
public_artifactory: true
os: ubuntu-22.04
compiler: clang-14
compiler: clang-18
cmake-version: 3.24.0
conan-version: 2.3.1
secrets:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(
DESCRIPTION
"This template library is a collection of template-oriented code that we, the Data Science Group at UPB, found pretty handy. It contains: `switch_cases` (Use runtime values in compile-time context), `integral_template_tuple` (Create a tuple-like structure that instantiates a template for a range of values), `integral_template_variant` (A wrapper type for `std::variant` guarantees to only contain variants of the form `T<IX>` and `for_{types,values,range}` (Compile time for loops for types, values or ranges))."
HOMEPAGE_URL "https://dice-research.org/")
set(POBR_VERSION 1) # Persisted Object Binary Representation
set(POBR_VERSION 2) # Persisted Object Binary Representation
liss-h marked this conversation as resolved.
Show resolved Hide resolved

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/dice/template-library/version.hpp)

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ It contains:
- `tuple_algorithms`: Some algorithms for iterating tuples
- `generator`: The reference implementation of `std::generator` from [P2502R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2502r2.pdf)
- `channel`: A single producer, single consumer queue
- `variant2`: Like `std::variant` but optimized for exactly two types

## Usage

Expand Down Expand Up @@ -87,6 +88,11 @@ all generator related things under namespace `std::`.
A single producer, single consume queue. This can be used to communicate between threads in a more high level
fashion than a mutex+container would allow.

### `variant2`
Like `std::variant` but specifically optimized for usage with two types/variants.
The internal representation is a `union` of the two types plus a 1 byte (3 state) discriminant.
Additionally, `visit` does not involve any virtual function calls.

### Further Examples

Compilable code examples can be found in [examples](./examples). The example build requires the cmake
Expand Down
10 changes: 9 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(examples_for examples_for.cpp)
Expand Down Expand Up @@ -72,3 +72,11 @@ target_link_libraries(example_channel
PRIVATE
dice-template-library::dice-template-library
)

add_executable(example_variant2
example_variant2.cpp)
target_link_libraries(example_variant2
PRIVATE
dice-template-library::dice-template-library
)

41 changes: 41 additions & 0 deletions examples/example_variant2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <cassert>
#include <string>

#include <dice/template-library/variant2.hpp>

namespace dtl = dice::template_library;
using namespace std::literals;

int main() {
// example adapted from from https://en.cppreference.com/w/cpp/utility/variant

dtl::variant2<int, float> v;
dtl::variant2<int, float> w;

v = 42; // v contains int
int i = dtl::get<int>(v);
assert(42 == i); // succeeds
w = dtl::get<int>(v);
w = dtl::get<0>(v); // same effect as the previous line
w = v; // same effect as the previous line

// dtl::get<double>(v); // error: no double in [int, float]
// dtl::get<3>(v); // error: valid index values are 0 and 1

try {
(void) dtl::get<float>(w); // w contains int, not float: will throw
}
catch (dtl::bad_variant_access const &ex) {
assert(ex.what() == "bad variant access"s);
}

dtl::variant<std::string> x("abc");
// converting constructors work when unambiguous
x = "def"; // converting assignment also works when unambiguous

dtl::variant<std::string, void const*> y("abc");
// casts to void const* when passed a char const*
assert(dtl::holds_alternative<void const*>(y)); // succeeds
y = "xyz"s;
assert(dtl::holds_alternative<std::string>(y)); // succeeds
}
Loading
Loading