Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::monostate

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      Defined in header<variant>
      Defined in header<utility>
      (since C++26)
      struct monostate{};
      (since C++17)

      Unit type intended for use as a well-behaved empty alternative instd::variant. In particular, a variant of non-default-constructible types may liststd::monostate as its first alternative: this makes the variant itself default-constructible.

      Contents

      [edit]Member functions

      (constructor)
      (implicitly declared)
      trivial implicit default/copy/move constructor
      (public member function)
      (destructor)
      (implicitly declared)
      trivial implicit destructor
      (public member function)
      operator=
      (implicitly declared)
      trivial implicit copy/move assignment
      (public member function)

      [edit]Non-member functions

      std::operator==, !=, <, <=, >, >=, <=>(std::monostate)

      constexprbool operator==( monostate, monostate)noexcept{returntrue;}
      (1)(since C++17)
      (2)
      constexprbool operator!=( monostate, monostate)noexcept{returnfalse;}

      constexprbool operator<( monostate, monostate)noexcept{returnfalse;}
      constexprbool operator>( monostate, monostate)noexcept{returnfalse;}
      constexprbool operator<=( monostate, monostate)noexcept{returntrue;}

      constexprbool operator>=( monostate, monostate)noexcept{returntrue;}
      (since C++17)
      (until C++20)
      constexprstd::strong_ordering operator<=>( monostate, monostate)noexcept

      {
         return std::strong_ordering::equal;

      }
      (since C++20)

      All instances ofstd::monostate compare equal.

      The<,<=,>,>=, and!= operators aresynthesized fromoperator<=> andoperator== respectively.

      (since C++20)

      [edit]Helper classes

      std::hash<std::monostate>

      template<>
      structstd::hash<monostate>;
      (since C++17)

      Specializes thestd::hash algorithm forstd::monostate.

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <variant> struct S{    S(int i): i(i){}int i;}; int main(){// Without the monostate type this declaration will fail.// This is because S is not default-constructible.std::variant<std::monostate, S> var;assert(var.index()==0); try{        std::get<S>(var);// throws! We need to assign a value}catch(conststd::bad_variant_access& e){std::cout<< e.what()<<'\n';}     var=42;std::cout<<"std::get: "<< std::get<S>(var).i<<'\n'<<"std::hash: "<<std::hex<<std::showbase<<std::hash<std::monostate>{}(std::monostate{})<<'\n';}

      Possible output:

      std::get: wrong index for variantstd::get: 42std::hash: 0xffffffffffffe19f

      [edit]See also

      constructs thevariant object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/monostate&oldid=177935"

      [8]ページ先頭

      ©2009-2025 Movatter.jp