c 2b 2b require keyword

Solutions on MaxInterview for c 2b 2b require keyword by the best coders in the world

showing results for - "c 2b 2b require keyword"
Simon
09 Sep 2020
1#include <string>
2#include <cstddef>
3#include <concepts>
4 
5// Declaration of the concept "Hashable", which is satisfied by any type 'T'
6// such that for values 'a' of type 'T', the expression std::hash<T>{}(a)
7// compiles and its result is convertible to std::size_t
8template<typename T>
9concept Hashable = requires(T a) {
10    { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
11};
12 
13struct meow {};
14 
15// Constrained C++20 function template:
16template<Hashable T>
17void f(T) {}
18//
19// Alternative ways to apply the same constraint:
20// template<typename T>
21//    requires Hashable<T>
22// void f(T) {}
23//
24// template<typename T>
25// void f(T) requires Hashable<T> {}
26 
27int main() {
28  using std::operator""s;
29  f("abc"s); // OK, std::string satisfies Hashable
30//f(meow{}); // Error: meow does not satisfy Hashable
31}