473,545 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t p_min, int p_max, int p_value)
{
std::stringstre am strbuf;
strbuf << "The value " << p_value << " is not in the
range [" << p_min << ", " << p_max << "].";
m_msg = strbuf.str();
}

out_of_range(ou t_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 IntegerValidato r
{
bool IsInRange(int value)
{
return true;
}
};

template
<
int t_Min,
int t_Max struct IntegerValidato r<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 IntegerValidato r<t_Min, t_Max, true, true> : public IntegerValidato r<t_Min, t_Max, true, false> {
bool IsInRange(int value)
{
if(!::IntegerVa lidator<t_Min, t_Max, true,
false>::IsInRan ge(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 IntegerAttribut e : public IntegerValidato r<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:
IntegerAttribut e() : m_value(0), m_valid(true)
{
}

explicit IntegerAttribut e(int p_value) : m_value(0),
m_valid(false)
{
SetValue(p_valu e);
}

IntegerAttribut e const& operator = (int p_value)
{
SetValue(p_valu e);
return *this;
}

IntegerAttribut e const& operator = (IntegerAttribu te const&
p_src)
{
SetValue(p_src. m_value);
}

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

#endif // end IntegerAttribut e.h

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

typedef IntegerAttribut e<0> PositiveInteger ;
typedef IntegerAttribut e<INT_MIN, 0> NegativeInteger ;

typedef IntegerAttribut e<0, INT_MAX, true, true> PositiveInteger Ex;
typedef IntegerAttribut e<INT_MIN, 0, true, true> NegativeInteger Ex;

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

typedef IntegerAttribut e<0> PositiveInteger ;
typedef IntegerAttribut e<std::numeric_ limits<int>::mi n, 0>
NegativeInteger ;

typedef IntegerAttribut e<0, std::numeric_li mits<int>::max, true, true>
PositiveInteger Ex;
typedef IntegerAttribut e<std::numeric_ limits<int>::mi n, 0, true, true>
NegativeInteger Ex;

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 1969
In article <11************ **********@v46g 2000cwv.googleg roups.com>,
"Vijai Kalyan" <vi**********@g mail.com> wrote:
typedef IntegerAttribut e<0, std::numeric_li mits<int>::max, true, true>
PositiveInteger Ex;
typedef IntegerAttribut e<std::numeric_ limits<int>::mi n, 0, true, true>
NegativeInteger Ex;

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
2192
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 value (both numeric again) from my table and write this code
3
14913
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
8
10395
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 is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or...
37
2422
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 cannot add them without first converting them to values. What might be causing this?
22
2112
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. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name*...
20
3667
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 MyVariant = 2 Then MsgBox "MyVariant also equals the value 2" 160 If MyVariant = "" Then HowManyCopies = 1 170 If Not IsNumeric(MyVariant) Then...
1
2485
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 fine). There is then a combo box they can select a field from (eg CompanyID etc) and then the list box below that contains the values (eg Microsoft,...
2
3991
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 functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can...
9
3048
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
7434
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.