Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: find with hash set 
Cpp :: dinamic 
Cpp :: what do I return in int main() function c++ 
Cpp :: c++ program to convert celsius to fahrenheit 
Cpp :: c++ vector allocator example 
Cpp :: c++ map values range 
Cpp :: find node from pos linkedlist c++ 
Cpp :: last index of array c++ 
Cpp :: foo foo little dogs 
Cpp :: assegnare valori in c++ 
Cpp :: c/c++ pointers 
Cpp :: Corong_ExerciseNo3(1) 
Cpp :: default order in set in c++ 
Cpp :: C++ selectin file location using Win32 API 
Cpp :: output sum of a range 
Cpp :: determining a string is subsequence of another 
Cpp :: c++ qt qtreewidget lock first column 
Cpp :: easy way to learn file handling in c++ array 
Cpp :: check if number is positive or negative in cpp 
Cpp :: all in one c++ 
Cpp :: c++ to c converter online 
Cpp :: c++ call overriden method from constructor 
Cpp :: count substrings codechef solution in c++ 
Cpp :: set app icon qt 
Cpp :: c++ copy pointer vector to static 
Cpp :: dignità 
Cpp :: online c++ graphics compiler 
Cpp :: Lapindromes codechef solution in c++ 
Cpp :: cpp super 
Cpp :: how to implement stack 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =