View source code
Display the source code in std/variant.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

Template std.variant.tryVisit

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

template tryVisit(Handlers...) ;

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.

Contained Functions

NameDescription
tryVisit

Returns

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

Throws

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

Example

Algebraic!(int, string) variant;

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

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

Authors

Andrei Alexandrescu

License

Boost License 1.0.