BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlf_overloaded

Detailed Description

Provide a type for constructing overload sets.

Outline

Purpose

Provide a type for constructing overload sets.

Classes

See also
bslstl_variant, bdljsn_json

Description

This component provides a template class, bdlf::Overloaded, that allows you to construct callable objects that contain several function call overloads.

This is especially useful when dealing with variant objects, as they can be passed to std::visit or to bsl::visit, and depending on the type stored in the variant, the correct element of the overload set will be called.

This component requires C++17.

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

First, create a bsl::variant object that can contain several different types.

Definition bslstl_variant.h:3806

Next, Create an Overload object containing several options:

bdlf::Overloaded over{
[] (unsigned) {return 1;}
, [] (double) {return 2;}
, [] (const bsl::string&) {return 3;}
};
Definition bslstl_string.h:1252

Set the value of variant, and then call std::visit, passing the overload set and the variant. Check the return value to see that the right lambda was called.

v = 2U;
assert(1 == bsl::visit(over, v));
v = 2.0;
assert(2 == bsl::visit(over, v));
v = bsl::string("2.0");
assert(3 == bsl::visit(over, v));
bsl::invoke_result< t_VISITOR &, typenamebsl::variant_alternative< 0, t_VARIANT >::type & >::type visit(t_VISITOR &visitor, t_VARIANT &variant)
Definition bslstl_variant.h:2359
basic_string< char > string
Definition bslstl_string.h:844