00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // ----------------------------------------------------------------------------- 00016 // bad_variant_access.h 00017 // ----------------------------------------------------------------------------- 00018 // 00019 // This header file defines the `absl::bad_variant_access` type. 00020 00021 #ifndef ABSL_TYPES_BAD_VARIANT_ACCESS_H_ 00022 #define ABSL_TYPES_BAD_VARIANT_ACCESS_H_ 00023 00024 #include <stdexcept> 00025 00026 #include "absl/base/config.h" 00027 00028 #ifdef ABSL_HAVE_STD_VARIANT 00029 00030 #include <variant> 00031 00032 namespace absl { 00033 using std::bad_variant_access; 00034 } // namespace absl 00035 00036 #else // ABSL_HAVE_STD_VARIANT 00037 00038 namespace absl { 00039 00040 // ----------------------------------------------------------------------------- 00041 // bad_variant_access 00042 // ----------------------------------------------------------------------------- 00043 // 00044 // An `absl::bad_variant_access` type is an exception type that is thrown in 00045 // the following cases: 00046 // 00047 // * Calling `absl::get(absl::variant) with an index or type that does not 00048 // match the currently selected alternative type 00049 // * Calling `absl::visit on an `absl::variant` that is in the 00050 // `variant::valueless_by_exception` state. 00051 // 00052 // Example: 00053 // 00054 // absl::variant<int, std::string> v; 00055 // v = 1; 00056 // try { 00057 // absl::get<std::string>(v); 00058 // } catch(const absl::bad_variant_access& e) { 00059 // std::cout << "Bad variant access: " << e.what() << '\n'; 00060 // } 00061 class bad_variant_access : public std::exception { 00062 public: 00063 bad_variant_access() noexcept = default; 00064 ~bad_variant_access() override; 00065 const char* what() const noexcept override; 00066 }; 00067 00068 namespace variant_internal { 00069 00070 [[noreturn]] void ThrowBadVariantAccess(); 00071 [[noreturn]] void Rethrow(); 00072 00073 } // namespace variant_internal 00074 } // namespace absl 00075 00076 #endif // ABSL_HAVE_STD_VARIANT 00077 00078 #endif // ABSL_TYPES_BAD_VARIANT_ACCESS_H_