//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include // concept for pointer-like objects: template concept IsPointer = requires(T p) { *p; // operator * has to be valid p == nullptr; // can compare with nullptr {p < p} -> std::convertible_to; // < yields bool }; // maxValue() for plain values: auto maxValue(auto a, auto b) { return b < a ? a : b; } // maxValue() for pointers: auto maxValue(IsPointer auto a, IsPointer auto b) requires std::totally_ordered_with { return maxValue(*a, *b); // return maximum value of where the pointers refer to } int main() { int x = 42; int y = 77; std::cout << maxValue(x, y) << '\n'; // maximum value of ints std::cout << maxValue(&x, &y) << '\n'; // maximum value of where the pointers point to int* xp = &x; int* yp = &y; std::cout << maxValue(&xp, &yp) << '\n'; // maximum value of pointer to pointer double d = 49.9; std::cout << maxValue(xp, &d) << '\n'; // maximum value of int and double pointer }