diff --git a/examples/Jamfile.v2 b/examples/Jamfile.v2 index 57e76ef..bb28ba9 100644 --- a/examples/Jamfile.v2 +++ b/examples/Jamfile.v2 @@ -7,3 +7,4 @@ run books.cpp ; run point.cpp ; run portable.cpp ; run template.cpp : : : msvc-8.0:no ; +run point2.cpp ; diff --git a/examples/point2.cpp b/examples/point2.cpp new file mode 100644 index 0000000..709ce63 --- /dev/null +++ b/examples/point2.cpp @@ -0,0 +1,65 @@ + +// Copyright 2005 Daniel James. +// Copyright 2022 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +// Force use of assert. +#if defined(NDEBUG) +#undef NDEBUG +#endif + +#include +#include +#include +#include + +// This example illustrates how to use Boost.Describe to obtain +// automatic boost::hash support. For full details see the hash +// tutorial. + +#if defined(BOOST_DESCRIBE_CXX14) + +class point +{ + int x; + int y; + + BOOST_DESCRIBE_CLASS(point, (), (), (), (x, y)) + +public: + + point() : x(0), y(0) {} + point(int x, int y) : x(x), y(y) {} +}; + +using boost::describe::operators::operator==; +using boost::describe::operators::operator!=; + +int main() +{ + boost::hash point_hasher; + + point p1(0, 0); + point p2(1, 2); + point p3(4, 1); + point p4 = p1; + + assert(point_hasher(p1) == point_hasher(p4)); + + // These tests could legally fail, but if they did it'd be a pretty bad + // hash function. + assert(point_hasher(p1) != point_hasher(p2)); + assert(point_hasher(p1) != point_hasher(p3)); +} + +#else + +#include + +int main() +{ + std::puts( "This example requires C++14." ); +} + +#endif