1 // Copyright 2018 The Abseil Authors.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // -----------------------------------------------------------------------------
16 // bad_variant_access.h
17 // -----------------------------------------------------------------------------
19 // This header file defines the `absl::bad_variant_access` type.
21 #ifndef ABSL_TYPES_BAD_VARIANT_ACCESS_H_
22 #define ABSL_TYPES_BAD_VARIANT_ACCESS_H_
26 #include "absl/base/config.h"
28 #ifdef ABSL_HAVE_STD_VARIANT
33 using std::bad_variant_access;
36 #else // ABSL_HAVE_STD_VARIANT
40 // -----------------------------------------------------------------------------
42 // -----------------------------------------------------------------------------
44 // An `absl::bad_variant_access` type is an exception type that is thrown in
45 // the following cases:
47 // * Calling `absl::get(absl::variant) with an index or type that does not
48 // match the currently selected alternative type
49 // * Calling `absl::visit on an `absl::variant` that is in the
50 // `variant::valueless_by_exception` state.
54 // absl::variant<int, std::string> v;
57 // absl::get<std::string>(v);
58 // } catch(const absl::bad_variant_access& e) {
59 // std::cout << "Bad variant access: " << e.what() << '\n';
61 class bad_variant_access : public std::exception {
63 bad_variant_access() noexcept = default;
64 ~bad_variant_access() override;
65 const char* what() const noexcept override;
68 namespace variant_internal {
70 [[noreturn]] void ThrowBadVariantAccess();
71 [[noreturn]] void Rethrow();
73 } // namespace variant_internal
76 #endif // ABSL_HAVE_STD_VARIANT
78 #endif // ABSL_TYPES_BAD_VARIANT_ACCESS_H_