A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/cpp/docs/reference/spanner/2.11.0/classgoogle_1_1cloud_1_1spanner_1_1Value below:

Class Value (2.11.0) | C++ Client Libraries

Skip to main content Class Value (2.11.0)

Stay organized with collections Save and categorize content based on your preferences.

The Value class represents a type-safe, nullable Spanner value.

It is conceptually similar to a std::any except the only allowed types are those supported by Spanner, and a "null" value (similar to a std::any without a value) still has an associated type. The supported types are shown in the following table along with how they map to the Spanner types (https://cloud.google.com/spanner/docs/data-types):

[1] The type T may be any of the other supported types, except for ARRAY/std::vector.

Value is a regular C++ value type with support for copy, move, equality, etc. A default-constructed Value represents an empty value with no type.

Note: There is also a C++ type of CommitTimestamp that corresponds to a Cloud Spanner TIMESTAMP object for setting the commit timestamp on a column with the allow_commit_timestamp set to true in the schema. See Also

https://cloud.google.com/spanner/docs/commit-timestamp Callers may create instances by passing any of the supported values (shown in the table above) to the constructor. "Null" values are created using the MakeNullValue<T>() factory function or by passing an empty absl::optional<T> to the Value constructor..

Example

Using a non-null value.

std::string msg = "hello";
spanner::Value v(msg);
StatusOr<std::string> copy = v.get<std::string>();
if (copy) {
  std::cout << *copy;  // prints "hello"
}
Example

Using a null value.

spanner::Value v = spanner::MakeNullValue<std::int64_t>();
StatusOr<std::int64_t> i = v.get<std::int64_t>();
assert(!i.ok());  // Can't get the value because v is null
StatusOr < absl::optional<std::int64_t> j =
    v.get<absl::optional<std::int64_t>>();
assert(j.ok());  // OK because an empty option can represent the null
assert(!j->has_value());  // v held no value.
Nullness

All of the supported types (above) are "nullable". A null is created in one of two ways:

  1. Passing an absl::optional<T>() with no value to Value's constructor.
  2. Using the MakeNullValue<T>() helper function (defined below).

Nulls can be retrieved from a Value::get<T> by specifying the type T as an absl::optional<U>. The returned optional will either be empty (indicating null) or it will contain the actual value. See the documentation for Value::get<T> below for more details.

Spanner Arrays

Spanner arrays are represented in C++ as a std::vector<T>, where the type T may be any of the other allowed Spanner types, such as bool, std::int64_t, etc. The only exception is that arrays may not directly contain another array; to achieve a similar result you could create an array of a 1-element struct holding an array. The following examples show usage of arrays.

std::vector<std::int64_t> vec = {1, 2, 3, 4, 5};
spanner::Value v(vec);
auto copy = *v.get<std::vector<std::int64_t>>();
assert(vec == copy);
Spanner Structs

Spanner structs are represented in C++ as instances of std::tuple holding zero or more of the allowed Spanner types, such as bool, std::int64_t, std::vector, and even other std::tuple objects. Each tuple element corresponds to a single field in a Spanner STRUCT.

Spanner STRUCT fields may optionally contain a string indicating the field's name. Fields names may be empty, unique, or repeated. A named field may be specified as a tuple element of type std::pair<std::string, T>, where the pair's .first member indicates the field's name, and the .second member is any valid Spanner type T.

using Struct = std::tuple<bool, std::pair<std::string, std::int64_t>>;
Struct s  = {true, {"Foo", 42}};
spanner::Value v(s);
assert(s == *v.get<Struct>());
Note: While a STRUCT's (optional) field names are not part of its C++ type, they are part of its Spanner STRUCT type. Array's (i.e., std::vector) must contain a single element type, therefore it is an error to construct a std::vector of std::tuple objects with differently named fields. Constructors Value()

Constructs a Value that holds nothing.

All calls to get<T>() will return an error.

Value(Value const &) Parameter Name Description Value const &
Value(Value &&) Parameter Name Description Value &&
Value(bool)

Constructs an instance with the specified type and value.

Parameter Name Description v bool
Value(std::int64_t)

Constructs an instance with the specified type and value.

Parameter Name Description v std::int64_t
Value(double)

Constructs an instance with the specified type and value.

Parameter Name Description v double
Value(std::string)

Constructs an instance with the specified type and value.

Parameter Name Description v std::string
Value(Bytes)

Constructs an instance with the specified type and value.

Parameter Name Description v Bytes
Value(Json)

Constructs an instance with the specified type and value.

Parameter Name Description v Json
Value(JsonB)

Constructs an instance with the specified type and value.

Parameter Name Description v JsonB
Value(Numeric)

Constructs an instance with the specified type and value.

Parameter Name Description v Numeric
Value(PgNumeric)

Constructs an instance with the specified type and value.

Parameter Name Description v PgNumeric
Value(Timestamp)

Constructs an instance with the specified type and value.

Parameter Name Description v Timestamp
Value(CommitTimestamp)

Constructs an instance with the specified type and value.

Parameter Name Description v CommitTimestamp
Value(absl::CivilDay)

Constructs an instance with the specified type and value.

Parameter Name Description v absl::CivilDay
Value(int)

Constructs an instance from common C++ literal types that closely, though not exactly, match supported Spanner types.

An integer literal in C++ is of type int, which is not exactly an allowed Spanner type. This will be allowed but it will be implicitly up converted to a std::int64_t. Similarly, a C++ string literal will be implicitly converted to a std::string. For example:

spanner::Value v1(42);
assert(42 == *v1.get<std::int64_t>());

spanner::Value v2("hello");
assert("hello" == *v2.get<std::string>());
Parameter Name Description v int
Value(char const *)

Constructs an instance from common C++ literal types that closely, though not exactly, match supported Spanner types.

An integer literal in C++ is of type int, which is not exactly an allowed Spanner type. This will be allowed but it will be implicitly up converted to a std::int64_t. Similarly, a C++ string literal will be implicitly converted to a std::string. For example:

spanner::Value v1(42);
assert(42 == *v1.get<std::int64_t>());

spanner::Value v2("hello");
assert("hello" == *v2.get<std::string>());
Parameter Name Description v char const *
Value(absl::optional< T >)

Constructs a non-null instance if opt has a value, otherwise constructs a null instance with the specified type T.

Parameters Name Description opt absl::optional< T >
typename T
Value(std::vector< T >)

Constructs an instance from a Spanner ARRAY of the specified type and values.

The type T may be any valid type shown above, except vectors of vectors are not allowed.

Warning: If T is a std::tuple with field names (i.e., at least one of its element types is a std::pair<std::string, T>) then, all of the vector's elements must have exactly the same field names. Any mismatch in in field names results in undefined behavior. Parameters Name Description v std::vector< T >
typename T
Value(std::tuple< Ts... >)

Constructs an instance from a Spanner STRUCT with a type and values matching the given std::tuple.

Any STRUCT field may optionally have a name, which is specified as std::pair<std::string, T>.

Parameters Name Description tup std::tuple< Ts... >
typename...
Operators operator=(Value const &) Parameter Name Description Value const &
Returns Type Description Value & operator=(Value &&) Parameter Name Description Value &&
Returns Type Description Value & Functions get() const &

Returns the contained value wrapped in a google::cloud::StatusOr<T>.

Returns a non-OK status IFF:

Example
spanner::Value v{3.14};
StatusOr<double> d = v.get<double>();
if (d) {
  std::cout << "d=" << *d;
}

// Now using a "null" std::int64_t
v = spanner::MakeNullValue<std::int64_t>();
StatusOr<std::int64_t> i = v.get<std::int64_t>();
if (!i) {
  std::cerr << "Could not get integer: " << i.status();
}
StatusOr<absl::optional<std::int64_t>> j =
    v.get<absl::optional<std::int64_t>>();
assert(j.ok());  // Since we know the types match in this example
assert(!v->has_value());  // Since we know v was null in this example
Parameter Name Description typename T
Returns Type Description StatusOr< T > get() &&

Returns the contained value wrapped in a google::cloud::StatusOr<T>.

Returns a non-OK status IFF:

Example
spanner::Value v{3.14};
StatusOr<double> d = v.get<double>();
if (d) {
  std::cout << "d=" << *d;
}

// Now using a "null" std::int64_t
v = spanner::MakeNullValue<std::int64_t>();
StatusOr<std::int64_t> i = v.get<std::int64_t>();
if (!i) {
  std::cerr << "Could not get integer: " << i.status();
}
StatusOr<absl::optional<std::int64_t>> j =
    v.get<absl::optional<std::int64_t>>();
assert(j.ok());  // Since we know the types match in this example
assert(!v->has_value());  // Since we know v was null in this example
Parameter Name Description typename T
Returns Type Description StatusOr< T >

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-14 UTC.

[[["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-08-14 UTC."],[[["The `Value` class in Spanner represents a type-safe, nullable value that is similar to `std::any` but only allows Spanner-supported types."],["Supported Spanner types include `BOOL`, `INT64`, `FLOAT64`, `STRING`, `BYTES`, `JSON`, `JSONB`, `NUMERIC`, `NUMERIC(PG)`, `TIMESTAMP`, `DATE`, `ARRAY`, and `STRUCT`, each corresponding to a specific C++ type."],["\"Null\" values can be created using the `MakeNullValue\u003cT\u003e()` factory function or by passing an empty `absl::optional\u003cT\u003e` to the `Value` constructor, and they can be retrieved by using `get\u003cabsl::optional\u003cU\u003e\u003e`."],["Spanner arrays are represented as `std::vector\u003cT\u003e` in C++, while Spanner structs are represented as `std::tuple\u003cTs...\u003e`, and fields in structs can optionally have a name of type `std::pair\u003cstd::string, T\u003e`."],["The `Value` class offers various constructors to create instances from different types, and the `get\u003cT\u003e()` method allows retrieving the contained value, with error handling for null values or type mismatches."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4