constrained_value type for ranged integers etc.

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

I have written a constrained_value type for things like range checking
integers, non-negative doubles, etc. and I have made the source code freely
available for all at http://www.cdiggins.com/constrained_value.hpp . Below
is a small example of the type in action:

#include "constrained_value.hpp"
#include <iostream>
typedef cv::constrained_value < cv::policies::ranged_integer < 0, 59 > >
Base60_A;
typedef cv::constrained_value < cv::policies::ranged_integer < 0, 59,
cv::policies::saturating > > Base60_B;

int main()
{
Base60_A a = 10;
a = a + 20;
try {
std::cout << "exception should be thrown" << std::endl;
a = a + 40;
std::cout << "no exception thrown" << std::endl;
}
catch(...) {
std::cout << "exception caught" << std::endl;
}
Base60_B b = 10;
std::cout << "Output should be 10" << std::endl << b << std::endl;
b = b + 70;
std::cout << "Output should be 59" << std::endl << b << std::endl;
b = b - 100;
std::cout << "Output should be 0" << std::endl << b << std::endl;

std::cin.get();
return 0;
};

There is a more complete demo of the type at
http://www.cdiggins.com/cv_test.cpp .
Enjoy!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top