ImFusion SDK 4.3
Variant< Types > Struct Template Reference

#include <ImFusion/Core/Utils/Variant.h>

Specialization of std::variant offering additional convenience member functions for functionality that the standard library only provides in the form of free functions. More...

+ Inheritance diagram for Variant< Types >:

Detailed Description

template<typename... Types>
struct ImFusion::Utils::Variant< Types >

Specialization of std::variant offering additional convenience member functions for functionality that the standard library only provides in the form of free functions.

Due to design decisions of the C++ committee interaction with a std::variant requires extensive usage of free functions such as std::get_if<T>() or std::holds_alternative<T>(), which may be disliked for stylistic reasons by some people and also be more difficult to find for people unfamiliar with the interface.

Example:

if (var.holds<int>())
std::cout << "variant holds an int\n";
else if (var.equals(42.f))
std::cout << "variant holds a float with value 42.f\n";
else if (std::string* s = var.getIf<std::string>())
std::cout << "variant holds the following string: " << *s << '\n';
var.visit([](auto&& val) { std::cout << "variant holds: " << val << '\n'; });
Specialization of std::variant offering additional convenience member functions for functionality tha...
Definition Variant.h:49
constexpr auto visit(Visitor &&visitor)
Applies the set of functions in visitor to this variant, picking the overload matching the current al...
Definition Variant.h:102
constexpr bool holds() const noexcept
Checks if the variant holds a value of type T.
Definition Variant.h:60
constexpr bool equals(const T &value) const noexcept
Checks if the variant holds a value of type T that equals value.
Definition Variant.h:69
constexpr T * getIf() noexcept
Attempts to retrieve the held value of type T as pointer or returns nullptr otherwise.
Definition Variant.h:78

Obviously, Utils::Variant behaves exactly like the underlying std::variant and you can still apply the standard library's free functions directly on it.

Note
Prior to C++23 an under-specification of the standard did not require std::visit() to work with classes such as our Utils::Variant. Fortunately, this was fixed with P2162. We tested our implementations for all our target platforms:
  • MSVC versions 19.20 (VS 2019) and newer
  • GCC's libstdc++ versions 7 and newer
  • Clang's libc++ versions 5 and newer
See also

Public Member Functions

 Variant (const std::variant< Types... > &rhs)
 
template<typename T>
constexpr bool holds () const noexcept
 Checks if the variant holds a value of type T.
 
template<typename T>
constexpr bool equals (const T &value) const noexcept
 Checks if the variant holds a value of type T that equals value.
 
template<typename T>
constexpr T * getIf () noexcept
 Attempts to retrieve the held value of type T as pointer or returns nullptr otherwise.
 
template<typename T>
constexpr const T * getIf () const noexcept
 Attempts to retrieve the held value of type T as pointer or returns nullptr otherwise.
 
template<class Visitor>
constexpr auto visit (Visitor &&visitor)
 Applies the set of functions in visitor to this variant, picking the overload matching the current alternative.
 
template<class Visitor>
constexpr auto visit (Visitor &&visitor) const
 Applies the set of functions in visitor to this variant, picking the overload matching the current alternative.
 
- Public Member Functions inherited from variant< Types... >
emplace (T... args)
 
index (T... args)
 
operator= (T... args)
 
swap (T... args)
 
valueless_by_exception (T... args)
 
variant (T... args)
 
~variant (T... args)
 

Member Function Documentation

◆ holds()

template<typename... Types>
template<typename T>
bool holds ( ) const
inlineconstexprnoexcept

Checks if the variant holds a value of type T.

T must appear exactly once in Types.

See also
std::holds_alternative()

◆ equals()

template<typename... Types>
template<typename T>
bool equals ( const T & value) const
inlineconstexprnoexcept

Checks if the variant holds a value of type T that equals value.

This is a convenience function for v.holds<T>() && *v.getIf<T>() == value. T must appear exactly once in Types.

◆ getIf() [1/2]

template<typename... Types>
template<typename T>
T * getIf ( )
inlineconstexprnoexcept

Attempts to retrieve the held value of type T as pointer or returns nullptr otherwise.

T must appear exactly once in Types.

See also
std::get_if<T>()

◆ getIf() [2/2]

template<typename... Types>
template<typename T>
const T * getIf ( ) const
inlineconstexprnoexcept

Attempts to retrieve the held value of type T as pointer or returns nullptr otherwise.

T must appear exactly once in Types.

See also
std::get_if<T>()

◆ visit() [1/2]

template<typename... Types>
template<class Visitor>
auto visit ( Visitor && visitor)
inlineconstexpr

Applies the set of functions in visitor to this variant, picking the overload matching the current alternative.

The visitor must accept every possible Type in Types. Example:

[](int i) { std::cout << "variant contains an int: "; },
[](const std::string& s) { "variant contains a string: "; },
});
var.visit([](auto&& value) { std::cout << value << '\n'; });
Syntactic sugar for easier implementation of visitors of a std::variant or our Utils::Variant.
Definition Overloaded.h:30
See also
Utils::Overloaded, std::visit()

◆ visit() [2/2]

template<typename... Types>
template<class Visitor>
auto visit ( Visitor && visitor) const
inlineconstexpr

Applies the set of functions in visitor to this variant, picking the overload matching the current alternative.

The visitor must accept every possible Type in Types. Example:

[](int i) { std::cout << "variant contains an int: "; },
[](const std::string& s) { "variant contains a string: "; },
});
var.visit([](auto&& value) { std::cout << value << '\n'; });
See also
Utils::Overloaded, std::visit()

The documentation for this struct was generated from the following file:
Search Tab / S to search, Esc to close