A class that holds option structs indexed by their type.
An "Option" is any struct that has a publicTypemember typedef. By convention they are named like "FooOption". Each library (e.g., spanner, storage) may define their own set of options. Additionally, there are common options defined that many libraries may use. All these options may be set in a singleOptionsinstance, and each library will look at the options that it needs.
Here's an overview of this class's interface, but see the method documentation below for details.
.set<T>(x)– Sets the optionTto valuex
.has<T>()– Returns true iff optionTis set
.unset<T>()– Removes the optionT
.get<T>()– Gets a const-ref to the value of optionT
.lookup<T>(x)– Gets a non-const-ref to optionT's value, initializing it toxif it was not set (xis optional).
Example:
struct FooOption {
using Type = int;
};
struct BarOption {
using Type = std::set<std::string>;
};
...
Options opts;
assert(opts.get<FooOption>() == 0);
opts.set<FooOption>(42);
assert(opts.get<FooOption>() == 42);
// Inserts two elements directly into the BarOption's std::set.
opts.lookup<BarOption>().insert("hello");
opts.lookup<BarOption>().insert("world");
std::set<std::string> const& bar = opts.get<BarOption>();
assert(bar == std::set<std::string>{"hello", "world"});
Constructors
Options()
Constructs an empty instance.
Options(Options const &)
Parameter
Name
Description
rhs
Options const &
Options(Options &&)
Parameter
Name
Description
rhs
Options &&
Operators
operator=(Options const &)
Parameter
Name
Description
rhs
Options const &
Returns
Type
Description
Options &
operator=(Options &&)
Parameter
Name
Description
rhs
Options &&
Returns
Type
Description
Options &
Functions
set(ValueTypeT< T >) &
Sets optionTto the valuevand returns a reference to*this.
struct FooOption {
using Type = int;
};
auto opts = Options{};
opts.set<FooOption>(123);
Parameters
Name
Description
v
ValueTypeT< T >
the value to set the option T
typename T
the option type
Returns
Type
Description
Options &
set(ValueTypeT< T >) &&
Sets optionTto the valuevand returns a reference to*this.
struct FooOption {
using Type = int;
};
auto opts = Options{}.set<FooOption>(123);
Parameters
Name
Description
v
ValueTypeT< T >
the value to set the option T
typename T
the option type
Returns
Type
Description
Options &&
has() const
Returns true IFF an option with typeTexists.
Parameter
Name
Description
typename T
the option type
Returns
Type
Description
bool
unset()
Erases the option specified by the typeT.
Parameter
Name
Description
typename T
the option type
Returns
Type
Description
void
get() const
Returns a reference to the value forT, or a value-initialized default ifTwas not set.
This method will always return a reference to a valid value of the correct type for optionT, whether or notThas actually been set. Usehas<T>()to check whether or not the option has been set.
struct FooOption {
using Type = std::set<std::string>;
};
Options opts;
std::set<std::string> const& x = opts.get<FooOption>();
assert(x.empty());
assert(!x.has<FooOption>());
opts.set<FooOption>({"foo"});
assert(opts.get<FooOption>().size() == 1);
Parameter
Name
Description
typename T
the option type
Returns
Type
Description
ValueTypeT< T > const &
lookup(ValueTypeT< T >)
Returns a reference to the value for optionT, setting the value toinit_valueif necessary.
struct BigOption {
using Type = std::set<std::string>;
};
Options opts;
std::set<std::string>& x = opts.lookup<BigOption>();
assert(x.empty());
x.insert("foo");
opts.lookup<BigOption>().insert("bar");
assert(x.size() == 2);
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThe provided content is a documentation of the \u003ccode\u003eOptions\u003c/code\u003e class in C++, which manages a collection of configurable option structs.\u003c/p\u003e\n"],["\u003cp\u003eThis class allows setting, checking, and unsetting different option types using methods like \u003ccode\u003e.set<T>(x)\u003c/code\u003e, \u003ccode\u003e.has<T>()\u003c/code\u003e, and \u003ccode\u003e.unset<T>()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can access the value of an option using \u003ccode\u003e.get<T>()\u003c/code\u003e or directly modify it with \u003ccode\u003e.lookup<T>(x)\u003c/code\u003e, initializing with \u003ccode\u003ex\u003c/code\u003e if the option is not set.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation also contains the details on using the class's constructors and operators, providing an overview of the class interface.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version available is \u003ccode\u003e2.37.0-rc\u003c/code\u003e while \u003ccode\u003e2.20.0\u003c/code\u003e is indicated as being the version for the documentation provided.\u003c/p\u003e\n"]]],[],null,["# Class Options (2.20.0)\n\nVersion 2.20.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Options)\n- [2.41.0](/cpp/docs/reference/common/2.41.0/classgoogle_1_1cloud_1_1Options)\n- [2.40.0](/cpp/docs/reference/common/2.40.0/classgoogle_1_1cloud_1_1Options)\n- [2.39.0](/cpp/docs/reference/common/2.39.0/classgoogle_1_1cloud_1_1Options)\n- [2.38.0](/cpp/docs/reference/common/2.38.0/classgoogle_1_1cloud_1_1Options)\n- [2.37.0](/cpp/docs/reference/common/2.37.0/classgoogle_1_1cloud_1_1Options)\n- [2.36.0](/cpp/docs/reference/common/2.36.0/classgoogle_1_1cloud_1_1Options)\n- [2.35.0](/cpp/docs/reference/common/2.35.0/classgoogle_1_1cloud_1_1Options)\n- [2.34.0](/cpp/docs/reference/common/2.34.0/classgoogle_1_1cloud_1_1Options)\n- [2.33.0](/cpp/docs/reference/common/2.33.0/classgoogle_1_1cloud_1_1Options)\n- [2.32.0](/cpp/docs/reference/common/2.32.0/classgoogle_1_1cloud_1_1Options)\n- [2.31.0](/cpp/docs/reference/common/2.31.0/classgoogle_1_1cloud_1_1Options)\n- [2.30.0](/cpp/docs/reference/common/2.30.0/classgoogle_1_1cloud_1_1Options)\n- [2.29.0](/cpp/docs/reference/common/2.29.0/classgoogle_1_1cloud_1_1Options)\n- [2.28.0](/cpp/docs/reference/common/2.28.0/classgoogle_1_1cloud_1_1Options)\n- [2.27.0](/cpp/docs/reference/common/2.27.0/classgoogle_1_1cloud_1_1Options)\n- [2.26.0](/cpp/docs/reference/common/2.26.0/classgoogle_1_1cloud_1_1Options)\n- [2.25.1](/cpp/docs/reference/common/2.25.1/classgoogle_1_1cloud_1_1Options)\n- [2.24.0](/cpp/docs/reference/common/2.24.0/classgoogle_1_1cloud_1_1Options)\n- [2.23.0](/cpp/docs/reference/common/2.23.0/classgoogle_1_1cloud_1_1Options)\n- [2.22.1](/cpp/docs/reference/common/2.22.1/classgoogle_1_1cloud_1_1Options)\n- [2.21.0](/cpp/docs/reference/common/2.21.0/classgoogle_1_1cloud_1_1Options)\n- [2.20.0](/cpp/docs/reference/common/2.20.0/classgoogle_1_1cloud_1_1Options)\n- [2.19.0](/cpp/docs/reference/common/2.19.0/classgoogle_1_1cloud_1_1Options)\n- [2.18.0](/cpp/docs/reference/common/2.18.0/classgoogle_1_1cloud_1_1Options)\n- [2.17.0](/cpp/docs/reference/common/2.17.0/classgoogle_1_1cloud_1_1Options)\n- [2.16.0](/cpp/docs/reference/common/2.16.0/classgoogle_1_1cloud_1_1Options)\n- [2.15.1](/cpp/docs/reference/common/2.15.1/classgoogle_1_1cloud_1_1Options)\n- [2.14.0](/cpp/docs/reference/common/2.14.0/classgoogle_1_1cloud_1_1Options)\n- [2.13.0](/cpp/docs/reference/common/2.13.0/classgoogle_1_1cloud_1_1Options)\n- [2.12.0](/cpp/docs/reference/common/2.12.0/classgoogle_1_1cloud_1_1Options)\n- [2.11.0](/cpp/docs/reference/common/2.11.0/classgoogle_1_1cloud_1_1Options)\n- [2.10.1](/cpp/docs/reference/common/2.10.1/classgoogle_1_1cloud_1_1Options) \nA class that holds option structs indexed by their type. \nAn \"Option\" is any struct that has a public `Type` member typedef. By convention they are named like \"FooOption\". Each library (e.g., spanner, storage) may define their own set of options. Additionally, there are common options defined that many libraries may use. All these options may be set in a single [`Options`](/cpp/docs/reference/common/2.20.0/classgoogle_1_1cloud_1_1Options) instance, and each library will look at the options that it needs.\n\nHere's an overview of this class's interface, but see the method documentation below for details.\n\n- `.set\u003cT\u003e(x)`-- Sets the option `T` to value `x`\n- `.has\u003cT\u003e()`-- Returns true iff option `T` is set\n- `.unset\u003cT\u003e()`-- Removes the option `T`\n- `.get\u003cT\u003e()`-- Gets a const-ref to the value of option `T`\n- `.lookup\u003cT\u003e(x)`-- Gets a non-const-ref to option `T`'s value, initializing it to `x` if it was not set (`x` is optional).\n\n###### Example:\n\n struct FooOption {\n using Type = int;\n };\n struct BarOption {\n using Type = std::set\u003cstd::string\u003e;\n };\n ...\n Options opts;\n\n assert(opts.get\u003cFooOption\u003e() == 0);\n opts.set\u003cFooOption\u003e(42);\n assert(opts.get\u003cFooOption\u003e() == 42);\n\n // Inserts two elements directly into the BarOption's std::set.\n opts.lookup\u003cBarOption\u003e().insert(\"hello\");\n opts.lookup\u003cBarOption\u003e().insert(\"world\");\n\n std::set\u003cstd::string\u003e const& bar = opts.get\u003cBarOption\u003e();\n assert(bar == std::set\u003cstd::string\u003e{\"hello\", \"world\"});\n\nConstructors\n------------\n\n### Options()\n\nConstructs an empty instance.\n\n### Options(Options const \\&)\n\n### Options(Options \\&\\&)\n\nOperators\n---------\n\n### operator=(Options const \\&)\n\n### operator=(Options \\&\\&)\n\nFunctions\n---------\n\n### set(ValueTypeT\\\u003c T \\\u003e) \\&\n\nSets option `T` to the value `v` and returns a reference to `*this`. \n\n struct FooOption {\n using Type = int;\n };\n auto opts = Options{};\n opts.set\u003cFooOption\u003e(123);\n\n### set(ValueTypeT\\\u003c T \\\u003e) \\&\\&\n\nSets option `T` to the value `v` and returns a reference to `*this`. \n\n struct FooOption {\n using Type = int;\n };\n auto opts = Options{}.set\u003cFooOption\u003e(123);\n\n### has() const\n\nReturns true IFF an option with type `T` exists.\n\n### unset()\n\nErases the option specified by the type `T`.\n\n### get() const\n\nReturns a reference to the value for `T`, or a value-initialized default if `T` was not set. \nThis method will always return a reference to a valid value of the correct type for option `T`, whether or not `T` has actually been set. Use `has\u003cT\u003e()` to check whether or not the option has been set. \n\n struct FooOption {\n using Type = std::set\u003cstd::string\u003e;\n };\n Options opts;\n std::set\u003cstd::string\u003e const& x = opts.get\u003cFooOption\u003e();\n assert(x.empty());\n assert(!x.has\u003cFooOption\u003e());\n\n opts.set\u003cFooOption\u003e({\"foo\"});\n assert(opts.get\u003cFooOption\u003e().size() == 1);\n\n### lookup(ValueTypeT\\\u003c T \\\u003e)\n\nReturns a reference to the value for option `T`, setting the value to `init_value` if necessary. \n\n struct BigOption {\n using Type = std::set\u003cstd::string\u003e;\n };\n Options opts;\n std::set\u003cstd::string\u003e& x = opts.lookup\u003cBigOption\u003e();\n assert(x.empty());\n\n x.insert(\"foo\");\n opts.lookup\u003cBigOption\u003e().insert(\"bar\");\n assert(x.size() == 2);"]]