Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/concepts/regular"

From cppreference.com
< cpp‎ | concepts
(fix)
Line 8: Line 8:
 
The {{tt|regular}} concept specifies that a type is ''regular'', that is, it is copyable, default constructible, and equality comparable. It is satisfied by types that behave similarly to built-in types like {{c|int}}, and that are comparable with {{tt|{{==}}}}.
 
The {{tt|regular}} concept specifies that a type is ''regular'', that is, it is copyable, default constructible, and equality comparable. It is satisfied by types that behave similarly to built-in types like {{c|int}}, and that are comparable with {{tt|{{==}}}}.
  
{{langlinks|ja|zh}}
+
===Example===
 +
{{example|code=
 +
#include <concepts>
 +
#include <iostream>
  
{{tt|Example for Regular}}
+
template<std::regular T>
 +
struct Single {
 +
    T value;
 +
    friend bool operator==(const Single&, const Single&) = default;
 +
};
  
          template<regular T>
+
int main()
                struct Single
+
{
                {
+
    Single<int> myInt1{4};
                      T value;
+
    Single<int> myInt2;
                };
+
    myInt2 = myInt1;
         
+
          #include <concepts>
+
          #include <iostream>
+
          int main(){
+
              Single<int> myInt1(4);
+
              Single<int> myInt2;
+
              myInt2 = myInt1;
+
              if(myInt1 == myInt2)
+
                  std::cout << "TRUE" << std::endl;
+
           
+
              std::cout << myInt1 << ' ' << myInt2 << std::endl;
+
            return 0;
+
          }
+
  
          OutPut:
+
    if (myInt1 == myInt2)
            True
+
        std::cout << "Equal\n";
            4 4
+
 
--[[User:Dotyes|Dotyes]] ([[User talk:Dotyes|talk]]) 04:51, 22 September 2019 (PDT)Mohan
+
    std::cout << myInt1.value << ' ' << myInt2.value << '\n';
 +
}
 +
|output=
 +
Equal
 +
4 4
 +
}}
 +
 
 +
{{langlinks|ja|zh}}

Revision as of 06:29, 22 September 2019

Defined in header <concepts>
template <class T>
concept regular = std::semiregular<T> && std::equality_comparable<T>;
(since C++20)

The regular concept specifies that a type is regular, that is, it is copyable, default constructible, and equality comparable. It is satisfied by types that behave similarly to built-in types like int, and that are comparable with ==.

Example

#include <concepts>
#include <iostream>
 
template<std::regular T>
struct Single {
    T value;
    friend bool operator==(const Single&, const Single&) = default;
};
 
int main()
{
    Single<int> myInt1{4};
    Single<int> myInt2;
    myInt2 = myInt1;
 
    if (myInt1 == myInt2)
        std::cout << "Equal\n";
 
    std::cout << myInt1.value << ' ' << myInt2.value << '\n';
}

Output:

Equal
4 4