473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 1963
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.