Horje
c++ require keyword Code Example
c++ require keyword
#include <string>
#include <cstddef>
#include <concepts>
 
// Declaration of the concept "Hashable", which is satisfied by any type 'T'
// such that for values 'a' of type 'T', the expression std::hash<T>{}(a)
// compiles and its result is convertible to std::size_t
template<typename T>
concept Hashable = requires(T a) {
    { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
};
 
struct meow {};
 
// Constrained C++20 function template:
template<Hashable T>
void f(T) {}
//
// Alternative ways to apply the same constraint:
// template<typename T>
//    requires Hashable<T>
// void f(T) {}
//
// template<typename T>
// void f(T) requires Hashable<T> {}
 
int main() {
  using std::operator""s;
  f("abc"s); // OK, std::string satisfies Hashable
//f(meow{}); // Error: meow does not satisfy Hashable
}




Cpp

Related
varint index Code Example varint index Code Example
empty 2d array as a member of a class class c++ Code Example empty 2d array as a member of a class class c++ Code Example
how to call subclass override method in c++ Code Example how to call subclass override method in c++ Code Example
math in section title latex Code Example math in section title latex Code Example
prefix using stack Code Example prefix using stack Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7