View source code Display the source code in std/variant.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Template std.variant.tryVisit

Behaves as visit but doesn't enforce that all types are handled by the visiting functions.

If a parameter-less function is specified it is called when either variant doesn't hold a value or holds a type which isn't handled by the visiting functions.

Arguments

template tryVisit(Handler...);

Functions

Function name Description
tryVisit

Returns

The return type of tryVisit is deduced from the visiting functions and must be the same across all overloads.

Throws

If no parameter-less, error function is specified: VariantException if variant doesn't hold a value or if variant holds a value which isn't handled by the visiting functions.

Example

Algebraic!(int, string) variant;

variant = 10;
auto which = -1;
variant.tryVisit!((int i) { which = 0; })();
assert(which == 0);

// Error function usage
variant = "test";
variant.tryVisit!((int i) { which = 0; },
                  ()      { which = -100; })();
assert(which == -100);

Authors

Andrei Alexandrescu

License

Boost License 1.0.

Comments