472,958 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Why are the min and max values for a particular numeric type implemented as functions?

Oops, maybe that is the standard. I don't have a copy unfortunately,
but here goes. I was experimenting and wrote the following code:

#include <limits.h>
#include <stdexcept>
#include <sstream>

namespace exceptions
{
class out_of_range : public std::exception
{
std::string m_msg;
protected:

out_of_range const& operator = (out_of_range const&);

public:
out_of_range(int p_min, int p_max, int p_value)
{
std::stringstream strbuf;
strbuf << "The value " << p_value << " is not in the
range [" << p_min << ", " << p_max << "].";
m_msg = strbuf.str();
}

out_of_range(out_of_range const& src)
{
m_msg = src.m_msg;
}

char const* what() const
{
return m_msg.c_str();
}
};
};

template
<
int t_Min,
int t_Max,
bool t_Validate,
bool t_useExceptions
struct IntegerValidator
{
bool IsInRange(int value)
{
return true;
}
};

template
<
int t_Min,
int t_Max struct IntegerValidator<t_Min, t_Max, true, false> {
bool IsInRange(int value)
{
return ((value <= t_Max) && (value >= t_Min));
}
};

template
<
int t_Min,
int t_Max struct IntegerValidator<t_Min, t_Max, true, true> : public IntegerValidator<t_Min, t_Max, true, false> {
bool IsInRange(int value)
{
if(!::IntegerValidator<t_Min, t_Max, true,
false>::IsInRange(value))
{
throw exceptions::out_of_range(t_Min, t_Max, value);
}
return true;
}
};

template
<
int t_Min = INT_MIN,
int t_Max = INT_MAX,
bool t_Validate = true,
bool t_useExceptions = false

class IntegerAttribute : public IntegerValidator<t_Min, t_Max,
t_Validate, t_useExceptions>
{
int m_value;
bool m_valid;
protected:
void SetValue(int p_value)
{
if(IsInRange(p_value))
{
m_value = p_value;
m_valid = true;
}
}
public:
IntegerAttribute() : m_value(0), m_valid(true)
{
}

explicit IntegerAttribute(int p_value) : m_value(0),
m_valid(false)
{
SetValue(p_value);
}

IntegerAttribute const& operator = (int p_value)
{
SetValue(p_value);
return *this;
}

IntegerAttribute const& operator = (IntegerAttribute const&
p_src)
{
SetValue(p_src.m_value);
}

operator int () const
{
return m_value;
}
};

#endif // end IntegerAttribute.h

just for fun. Here are now a few particular instantiations that I
tried:

typedef IntegerAttribute<0> PositiveInteger;
typedef IntegerAttribute<INT_MIN, 0> NegativeInteger;

typedef IntegerAttribute<0, INT_MAX, true, true> PositiveIntegerEx;
typedef IntegerAttribute<INT_MIN, 0, true, true> NegativeIntegerEx;

IMHO, I had to mix C style type ranges with C++. Shouldn't I ideally,
IMHO, have used

typedef IntegerAttribute<0> PositiveInteger;
typedef IntegerAttribute<std::numeric_limits<int>::min, 0>
NegativeInteger;

typedef IntegerAttribute<0, std::numeric_limits<int>::max, true, true>
PositiveIntegerEx;
typedef IntegerAttribute<std::numeric_limits<int>::min, 0, true, true>
NegativeIntegerEx;

instead. I cannot however do that because the above instantiations
require a compile time constant and min and max actually are functions.

Is this good? (Not from a style perspective, but from a usability
perspective.) Or am I missing something here?

Ultimately, anyway, in the particular compiler I am using the functions
min () and max() are anyway implemented as follows:

static _Ty (__CRTDECL min)() _THROW0()
{ // return minimum value
return (INT_MIN);
}

static _Ty (__CRTDECL max)() _THROW0()
{ // return maximum value
return (INT_MAX);
}

regards,

-vijai.

Mar 21 '06 #1
1 1938
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Vijai Kalyan" <vi**********@gmail.com> wrote:
typedef IntegerAttribute<0, std::numeric_limits<int>::max, true, true>
PositiveIntegerEx;
typedef IntegerAttribute<std::numeric_limits<int>::min, 0, true, true>
NegativeIntegerEx;

instead. I cannot however do that because the above instantiations
require a compile time constant and min and max actually are functions.

Is this good?


This is the unfortunate result of the fact that floating point constants
can't be defined in a class the way integral constants can, and
numeric_limits handles floating point types as well.

Yes, your compiler reflects the standard.

-Howard
Mar 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Savas Ates | last post by:
i have 3 columns in sql server .. all of them are numeric value when a user enter my site i take the value of userid (numeric one) i assign it as session("userid ") after a query i take 2...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
37
by: MLH | last post by:
For example: Nz(,0) returns "300" if the value in field is 300 (currency data type) and "0" if the value is zero or null. I get strings in the query output - they are all left aligned and I...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
1
by: Intrepid_Yellow | last post by:
Hi, I have the following code that runs my report generator. The user selects a table from a combo box, then whatever fields they want from a list box. (This part all works and the report runs...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
9
by: tom | last post by:
Hi! How can I determine the smallest and largest values of numeric types (for example int) possible in my system? I think there exists a function for this task but I don't know it.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.