* Add ranges * Initial Hugo setup * Work in progress * Added selection for local or remote site * Updated to 'light' theme * Changed to using Hextra Hugo theme * Changed to using Hextra Hugo theme * Changed to Hextra Hugo theme * Change to Hextra Hugo theme * Updated Hugo setup. * Updated Hugo setup. # Conflicts: # docs/releases/_index.md * Work in progress * Added new fonts Added new documentation * Latest documentation updates * Latest documentation updates # Conflicts: # docs/containers/array.md # docs/containers/array_view.md # docs/containers/array_wrapper.md # docs/containers/bip_buffer_spsc_atomic.md # docs/containers/bitset.md # docs/containers/indirect_vector.md # docs/containers/vector.md # docs/getting-started/compilers.md * Added bloom_filter markdown doc * Added more documentation Updated CSS for light and dark modes * Fixed some menus Added mode documentation files * Updated CSS rules Added badges to home page Added uniqur_ptr + pool tutorial * Fixed formatting on the home page markdown Modified light amd dark code formatting * Updated unique_ptr-with-pool * Added container and shared message tutorials * Updates to documentation * Added const_multimap * Updated source-formatting.md * Added initial raw text files form Web site editor * Innore coverage build directory * Exported raw text documentation files from the web site editor * Hugo updates * Added Hugo intalation and markdown descriptions * More addition to the documentation * Added closure.md and updates to delegate.md * Added format.md * Added documentation for etl::delegate_observable, etl::function, Base64 codec * Added io_port documentation * Added basic_format_spec * Added documentation for string_stream and string utilities. * Added more documentation Updated the documentation CSS * Added documentation for clocks, day, duration * Added more documentation for chrono classes Updated callouts * More chrono documentation * Completed chrono documentation * Maths functions documentation * Completed maths documentation * Completed maths documentation * Completed maths documentation * Completed maths documentation * Added multiple documentation files * Added iterator.md * Added debug_count.md and versions.md * Added debug_count.md and versions.md * Added more documentation * More documentation * Added some design pattern documentation Modified some of the layout files Modified the About documentation * Converted more documentation pages Modified the site CSS * Added more documentation Moced some documentation files to new directories * Added more documentation Tweaks to CSS * Added callback_timer_deferred_locked documentation * Added callback_timer_locked documentation * More documentation updates * More documentation updates * More documentation updates * New documentation files. Harmonised file name format * New documentation files. * Multiple document updates * Multiple document updates * Final conversion of web pages * Updates before PR * Updates before PR * Updates before PR # Conflicts: # docs/blog/_index.md * Final pre PR updates * Updates to message framework documentation * Renamed directory * Fix spelling * Added author and date to blog files Moved documentation files merged from development * Fixed 'Description' typo * Fix typos # Conflicts: # docs/IO/io_port.md # docs/containers/sets/const-multiset.md # docs/containers/sets/const-set.md # docs/maths/correlation.md # docs/maths/gamma.md * Renamed two files to lower case * Minor renaming * Added author and date * Updated callout on bresenham_line.md Added support for showing the ETL version on the documentation first page, by copying the version.txt file as a hugo asset. Updated the Python 'update_release.py' to copy 'version.txt' * Replace space in filename with hyphen. Added more information to hugo-commands.md * Replace space in filename with hyphen. Added more information to hugo-commands.md # Conflicts: # docs/getting-started/view-the-docs-locally/hugo-commands.md * Added a link to pseudo_moving_average.md * Updated title pages for groups * Fixed missing 404 for non-existent pages * Fixed coordinate variable names in the 'Calculating the intersection' example --------- Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de> Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com> Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
11 KiB
| title |
|---|
| variant |
{{< callout type="info">}}
Header: variant.h
Similar to: std::variant
{{< /callout >}}
For C++11 and above.
template <typename... TTypes>
class variant
A variadic class that can contain one a several specified types in a type safe manner.
C++11 and C++14 support up to 32 types for visitor callback callbacks.
C++17 and above supports any number of visitor callback types.
Accepts functors, etl::overload and etl::visitor types as visitor callbacks.
Accepts etl::visit since 20.28.0.
Error exception types
class variant_exception
Description
The base for all variant exceptions.
Derived from etl::exception.
class variant_incorrect_type_exception
Description
The error types emitted when a non-supported type is requested from the variant.
Construction
ETL_CONSTEXPR14 variant()
Description
Default constructor.
template <typename T>
ETL_CONSTEXPR14 variant(T&& value)
Description
Constructor from a value.
template <typename T, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_type_t<T>, TArgs&&... args)
Description
Construct T from arguments.
template <size_t Index, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t<Index>, TArgs&&... args)
Description
Construct from Index using args.
template <typename T, typename U, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_type_t<T>,
std::initializer_list<U> init,
TArgs&&... args)
Description
Construct from type, initializer_list and arguments.
template <size_t Index, typename U, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t<Index>,
std::initializer_list<U> init,
TArgs&&... args)
Description
Construct from Index, initializer_list and arguments.
ETL_CONSTEXPR14 variant(const variant& other)
Description
Copy constructor.
ETL_CONSTEXPR14 variant(variant&& other)
Description
Move constructor.
Operations
template <typename T, typename... TArgs>
T& emplace(TArgs&&... args)
Description
Set to type T using the construction parameters args.
template <typename T>
variant& operator =(T&& value)
Description
variant& operator =(const variant& other)
Description
Copies other to the current variant.
variant& operator =(variant&& other)
Description
Moves other to the current variant.
constexpr bool valueless_by_exception() const noexcept
Description
Returns true if the type id is etl::variant_npos.
constexpr size_t index() const noexcept
Description
Returns the index of the type contained in the variant.
Returns etl::variant_npos if the variant does not hold a valid type.
void swap(variant& rhs) noexcept
Description
Swaps rhs with the current variant.
Access
Non-member functions
get
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::variant_alternative_t<Index, etl::variant<TTypes...>>&
get(etl::variant<TTypes...>& v)
Description
Get the value based on Index.
If Index matches the current index of the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
A reference to a variant.
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::variant_alternative_t<Index, etl::variant<TTypes...>>&&
get(etl::variant<TTypes...>&& v)
Description
Get the value based on Index.
If Index matches the current index of the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
An rvalue reference to a variant.
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 const etl::variant_alternative_t<Index, const etl::variant<TTypes...>>&
get(const etl::variant<TTypes...>& v)
Description
Get the value based on Index.
If Index matches the current index of the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
A const reference to a variant.
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 const etl::variant_alternative_t<Index, const etl::variant<TTypes...>>&&
get(const etl::variant<TTypes...>&& v)
Description
Get the value based on Index.
If Index matches the current index of the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
A const rvalue reference to a variant.
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 T& get(etl::variant<TTypes...>& v)
Description
Get the value based on T.
If T matches the current type stored in the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
A reference to a variant.
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 T&& get(etl::variant<TTypes...>&& v)
Description
Get the value based on T.
If T matches the current type stored in the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
An rvalue reference to a variant.
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 const T& get(const etl::variant<TTypes...>& v)
Description
Get the value based on T.
If T matches the current type stored in the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
A const reference to a variant.
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 const T&& get(const etl::variant<TTypes...>&& v)
Description
Get the value based on T.
If T matches the current type stored in the variant, returns a reference to the stored value, otherwise, asserts etl::variant_incorrect_type_exception.
Parameters
A const rvalue reference to a variant.
get_if
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<etl::variant_alternative_t<Index, etl::variant<TTypes...>>>
get_if(etl::variant<TTypes...>* pv) noexcept
Description
Get the value based on Index.
If Index matches the current index of the variant, returns a pointer to the stored value, otherwise returns nullptr.
Parameters
A pointer to a variant.
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<const etl::variant_alternative_t<Index, etl::variant<TTypes...>>>
get_if(const etl::variant<TTypes...>* pv) noexcept
Description
Get the value based on Index.
If Index matches the current index of the variant, returns a const pointer to the stored value, otherwise returns nullptr.
Parameters
A const pointer to a variant.
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<T> get_if(etl::variant<TTypes...>* pv) noexcept
Description
Get the value based on T.
If T matches the current type stored in the variant, returns a pointer to the stored value, otherwise return nullptr.
Parameters
An pointer to a variant.
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<const T> get_if(const etl::variant<TTypes...>* pv) noexcept
Description
Get the value based on T.
If T matches the current type stored in the variant, returns a const pointer to the stored value, otherwise return nullptr.
Parameters
An const pointer to a variant.
Other
struct monostate
Description
A default constructible type.
For use as the first state to turn a non-default constructible variant into a default constructible variant.
template <typename... TTypes>
void swap(etl::variant<TTypes...>& lhs, etl::variant<TTypes...>& rhs)
Description
Swaps lhs and rhs.
template <typename... TTypes>
struct variant_size<etl::variant<TTypes...>>
Description
The member constant value contains the number of types.
For C++17 and above.
template <typename... TTypes>
inline constexpr size_t variant_size_v = variant_size<TTypes...>::value;
Description
Has the value equal to the number of types.
Tests
Non-member functions
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 bool holds_alternative(const etl::variant<TTypes...>& v) noexcept
Description
Checks if the variant v holds the alternative T.
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 bool holds_alternative(const etl::variant<TTypes...>& v) noexcept
Description
Checks if the variant v holds the alternative Index.
template <typename... TTypes>
ETL_CONSTEXPR14 bool holds_alternative(size_t index, const etl::variant<TTypes...>& v) noexcept
Description
Checks if the variant v holds the alternative index.
Visitation
For etl::visitor
For C++11 and C++14 the maximum number of visitor types is 32. Code size may be minimised if one of the following macros are defined.
ETL_VARIANT_CPP11_MAX_8_TYPES
ETL_VARIANT_CPP11_MAX_16_TYPES
ETL_VARIANT_CPP11_MAX_24_TYPES
Description
C++17 and above support any number of visitor callback types.
etl::variant provides member functions that support two methods of visitation.
void accept_visitor(etl::visitor<TTypes...>& v)
Description
Accepts an etl::visitor derived type.
Deprecated from 20.36.0
void accept_visitor(etl::visitor<TTypes...>& v) const
Description
Accepts an etl::visitor derived type.
Deprecated
Since: 20.36.0
void accept(etl::visitor<TTypes...>& v)
Description
Accepts an etl::visitor derived type.
Since: 20.36.0
void accept(etl::visitor<TTypes...>& v) const
Description
Accepts an etl::visitor derived type.
Since: 20.36.0
See visitor
template <typename TVisitor>
void accept_functor(TVisitor& v)
Description
Accepts a functor that overrides operator() for each type.
Deprecated since 20.36.0
template <typename TVisitor>
void accept_functor(TVisitor& v) const
Description
Accepts a functor that overrides operator() for each type.
Deprecated since 20.36.0
template <typename TVisitor>
void accept(TVisitor& v)
Description
Accepts a functor that overrides operator() for each type.
Since: 20.36.0
template <typename TVisitor>
void accept(TVisitor& v) const
Description
Accepts a functor that overrides operator() for each type.
Since: 20.36.0
See overload
Visitation
For STL style etl::visit.
Since: 20.28.0
The ETL implements an STL style visit function.