[variant.get] # 22 General utilities library [[utilities]](./#utilities) ## 22.6 Variants [[variant]](variant#get) ### 22.6.5 Value access [variant.get] [🔗](#lib:holds_alternative) `template constexpr bool holds_alternative(const variant& v) noexcept; ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6520) *Mandates*: The type T occurs exactly once in Types[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6524) *Returns*: true if index() is equal to the zero-based index of T in Types[.](#2.sentence-1) [🔗](#itemdecl:2) `template constexpr variant_alternative_t>& GET(variant& v); // exposition only template constexpr variant_alternative_t>&& GET(variant&& v); // exposition only template constexpr const variant_alternative_t>& GET(const variant& v); // exposition only template constexpr const variant_alternative_t>&& GET(const variant&& v); // exposition only ` [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6545) *Mandates*: I < sizeof...(Types)[.](#3.sentence-1) [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6549) *Preconditions*: v.index() is I[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6553) *Returns*: A reference to the object stored in the variant[.](#5.sentence-1) [🔗](#lib:get,variant) `template constexpr variant_alternative_t>& get(variant& v); template constexpr variant_alternative_t>&& get(variant&& v); template constexpr const variant_alternative_t>& get(const variant& v); template constexpr const variant_alternative_t>&& get(const variant&& v); ` [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6571) *Mandates*: I < sizeof...(Types)[.](#6.sentence-1) [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6575) *Effects*: If v.index() is I, returns a reference to the object stored in the variant[.](#7.sentence-1) Otherwise, throws an exception of type bad_variant_access[.](#7.sentence-2) [🔗](#lib:get,variant_) `template constexpr T& get(variant& v); template constexpr T&& get(variant&& v); template constexpr const T& get(const variant& v); template constexpr const T&& get(const variant&& v); ` [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6590) *Mandates*: The type T occurs exactly once in Types[.](#8.sentence-1) [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6594) *Effects*: If v holds a value of type T, returns a reference to that value[.](#9.sentence-1) Otherwise, throws an exception of type bad_variant_access[.](#9.sentence-2) [🔗](#lib:get_if) `template constexpr add_pointer_t>> get_if(variant* v) noexcept; template constexpr add_pointer_t>> get_if(const variant* v) noexcept; ` [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6612) *Mandates*: I < sizeof...(Types)[.](#10.sentence-1) [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6616) *Returns*: A pointer to the value stored in the variant, if v != nullptr and v->index() == I[.](#11.sentence-1) Otherwise, returns nullptr[.](#11.sentence-2) [🔗](#lib:get_if_) `template constexpr add_pointer_t get_if(variant* v) noexcept; template constexpr add_pointer_t get_if(const variant* v) noexcept; ` [12](#12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6634) *Mandates*: The type T occurs exactly once in Types[.](#12.sentence-1) [13](#13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L6638) *Effects*: Equivalent to: return get_if(v); with i being the zero-based index of T in Types[.](#13.sentence-1)