Write a program which will read a coordinate pair for a point in the x-
y plane. It should output whether the point is at the origin (0, 0),
on the X axis [such as (6, 0)], on the Y axis [such as (0, -2)], or in
one of four quadrants: for example: (3, 1) is in quadrant 1, (-2, 2)
is in quadrant 2, (-5, -10) is in quadrant 3, and (5, -1) is in
quadrant 4.
// Point.cpp
//
// To use: Compile with the point's coordinates defined as XPOS and YPOS
// and run the resulting file
// e.g. To test the origin (0, 0) run the following command
// (assuming you are using gcc):
// g++ Point.cpp -DXPOS=0 -DYPOS=0 && ./a.out
//
#include <iostream>
#include <string>
#include <boost/mpl/int.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/eval_if.hpp>
namespace mpl = boost::mpl;
typedef mpl::int_<XPOS> xpos;
typedef mpl::int_<YPOS> ypos;
typedef mpl::int_<0> zero;
typedef mpl::less<xpos, zero> x_less_0;
typedef mpl::equal_to<xpos, zero> x_equal_0;
typedef mpl::greater<xpos, zero> x_greater_0;
typedef mpl::less<ypos, zero> y_less_0;
typedef mpl::equal_to<ypos, zero> y_equal_0;
typedef mpl::greater<ypos, zero> y_greater_0;
enum { ORIGIN, X_AXIS, Y_AXIS, QUAD_1, QUAD_2, QUAD_3, QUAD_4 };
template <int msg> struct message { typedef message<msg> type; static
std::string value; };
template<> std::string message<ORIGIN>::value = "At the origin";
template<> std::string message<X_AXIS>::value = "On the x-axis";
template<> std::string message<Y_AXIS>::value = "On the y-axis";
template<> std::string message<QUAD_1>::value = "In quadrant 1";
template<> std::string message<QUAD_2>::value = "In quadrant 2";
template<> std::string message<QUAD_3>::value = "In quadrant 3";
template<> std::string message<QUAD_4>::value = "In quadrant 4";
typedef mpl::and_<x_equal_0, y_equal_0> at_origin;
typedef mpl::eval_if<x_less_0, message<QUAD_2>, message<QUAD_1> > q12_msg;
typedef mpl::eval_if<x_less_0, message<QUAD_3>, message<QUAD_4> > q34_msg;
typedef mpl::eval_if<y_greater_0, q12_msg, q34_msg> q_msg;
typedef mpl::eval_if<x_equal_0, message<Y_AXIS>, q_msg> ya_msg;
typedef mpl::eval_if<y_equal_0, message<X_AXIS>, ya_msg> aq_msg;
typedef mpl::eval_if<at_origin, message<ORIGIN>, aq_msg> msg;
int main() { std::cout << msg::type::value << std::endl; }
-Garrett
P.S. this is not meant to be an actual solution, I just wanted to
practice template metaprogramming.