• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Abseil Authors.
2 //
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
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 //
15 // -----------------------------------------------------------------------------
16 // bad_variant_access.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header file defines the `absl::bad_variant_access` type.
20 
21 #ifndef ABSL_TYPES_BAD_VARIANT_ACCESS_H_
22 #define ABSL_TYPES_BAD_VARIANT_ACCESS_H_
23 
24 #include <stdexcept>
25 
26 #include "absl/base/config.h"
27 
28 #ifdef ABSL_USES_STD_VARIANT
29 
30 #include <variant>
31 
32 namespace absl {
33 ABSL_NAMESPACE_BEGIN
34 using std::bad_variant_access;
35 ABSL_NAMESPACE_END
36 }  // namespace absl
37 
38 #else  // ABSL_USES_STD_VARIANT
39 
40 namespace absl {
41 ABSL_NAMESPACE_BEGIN
42 
43 // -----------------------------------------------------------------------------
44 // bad_variant_access
45 // -----------------------------------------------------------------------------
46 //
47 // An `absl::bad_variant_access` type is an exception type that is thrown in
48 // the following cases:
49 //
50 //   * Calling `absl::get(absl::variant) with an index or type that does not
51 //     match the currently selected alternative type
52 //   * Calling `absl::visit on an `absl::variant` that is in the
53 //     `variant::valueless_by_exception` state.
54 //
55 // Example:
56 //
57 //   absl::variant<int, std::string> v;
58 //   v = 1;
59 //   try {
60 //     absl::get<std::string>(v);
61 //   } catch(const absl::bad_variant_access& e) {
62 //     std::cout << "Bad variant access: " << e.what() << '\n';
63 //   }
64 class bad_variant_access : public std::exception {
65  public:
66   bad_variant_access() noexcept = default;
67   ~bad_variant_access() override;
68   const char* what() const noexcept override;
69 };
70 
71 namespace variant_internal {
72 
73 [[noreturn]] void ThrowBadVariantAccess();
74 [[noreturn]] void Rethrow();
75 
76 }  // namespace variant_internal
77 ABSL_NAMESPACE_END
78 }  // namespace absl
79 
80 #endif  // ABSL_USES_STD_VARIANT
81 
82 #endif  // ABSL_TYPES_BAD_VARIANT_ACCESS_H_
83