价值限制 [英] Value restriction

查看:84
本文介绍了价值限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里有点腌菜我一直在思考。我把它简化为

发布,这里是:

考虑一个简单(和哑)的函数如下:

bool IsAfter1998(年份)

{

返回(年份> 1998);

}


所以让我们来定义年。类型。我们从以下开始:

typedef int Year;

我想限制Year的值。不是0(公元1年跟随

到公元1年)。我已经开始了这个:

班级年份

{

私人:


int data;


void Set(int year)

{

if(year == 0)throw bad_year();


data = year;

}


public:


class bad_year {};


显式年份(int year = 1)

{

Set(year);

}


年& operator =(Year year)

{

Set(year);


return * this;

}


operator int()

{

返回数据;

}


};

我认为很多人之前可能做过类似的事情。所以

任何人都可以建议任何合适的替代方案或者可以调整我的代码

一点点吗?


特别是我的代码,我意识到那个人不能做到以下几点:


年份;


++年份;

- JKop


Here''s a little pickle I''ve been pondering over. I''ve simplified it down for
posting, here goes:
Consider a function as simple (and as dumb) as the following:
bool IsAfter1998(Year year)
{
return (year > 1998);
}

So let''s define the "Year" type. We start off with:
typedef int Year;
I want to restrict the value of a "Year" to not being 0 ( 1 BC is followed
by 1 AD). I''ve started off with this:
class Year
{
private:

int data;

void Set(int year)
{
if ( year == 0 ) throw bad_year();

data = year;
}

public:

class bad_year {};

explicit Year(int year = 1)
{
Set(year);
}

Year& operator=(Year year)
{
Set(year);

return *this;
}

operator int()
{
return data;
}

};
I figured a lot of people have probably done something like this before. So
can anyone suggest any suitable alternatives or maybe tweek my code a
little?

Particularly with my code, I realize that one can''t do the following:

Year year;

++year;
-JKop

推荐答案

JKop写道:
这是我一直在思考的小泡菜。我已经简化了
发布,这里是:

考虑一个简单(和哑)的函数如下:

bool IsAfter1998(年份)
{
返回(年> 1998);
}
所以让我们定义年类型。我们从以下开始:

typedef int Year;

我想限制Year的值。不是0(公元1年跟随公元前1年)。我已经开始了这个:

班级年份
{
私人:

int数据;

无效设置(int year)
{
if(year == 0)throw bad_year();

data = year;
}
public:

class bad_year {};

明确年份(int year = 1)
{
设定(年);
}

年& operator =(Year year)
{
设置(年);

返回* this;
}

operator int()<回复数据;

};

我认为很多人之前可能做过类似的事情。那么
任何人都可以建议任何合适的替代方案或者可能会调整我的代码吗

特别是对于我的代码,我意识到不能做到以下几点:

年份;

++ year;

-JKop
Here''s a little pickle I''ve been pondering over. I''ve simplified it down for
posting, here goes:
Consider a function as simple (and as dumb) as the following:
bool IsAfter1998(Year year)
{
return (year > 1998);
}

So let''s define the "Year" type. We start off with:
typedef int Year;
I want to restrict the value of a "Year" to not being 0 ( 1 BC is followed
by 1 AD). I''ve started off with this:
class Year
{
private:

int data;

void Set(int year)
{
if ( year == 0 ) throw bad_year();

data = year;
}

public:

class bad_year {};

explicit Year(int year = 1)
{
Set(year);
}

Year& operator=(Year year)
{
Set(year);

return *this;
}

operator int()
{
return data;
}

};
I figured a lot of people have probably done something like this before. So
can anyone suggest any suitable alternatives or maybe tweek my code a
little?

Particularly with my code, I realize that one can''t do the following:

Year year;

++year;
-JKop




我爱整数:

年度sci_fic;

sci_fic = -20;

sci_fic ++;

sci_fic - ;

sci_fic / = 40;


顺便说一句,你可以使用_unsigned_int_如果你想要编译器将年份限制为积极的 b br />
值。


一个想法是有一个内联函数验证

a年:

inline Validate_Year(unsigned int year)

{

if(year< 1)

throw Your_Favorite_Exception();

返回;

}


我认为这会产生最小的影响,因为它将b $ b声明为内联。


我认为没有必要通过

c复杂化代码在一个单独的年级课程。不,恕我直言,任何

特殊理由让这一年成为一个单独的类型。

-

托马斯马修斯

C ++新闻组欢迎辞:
http:/ /www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍



I love integers:
Year sci_fic;
sci_fic = -20;
sci_fic++;
sci_fic--;
sci_fic /= 40;

By the way, you could use _unsigned_int_ if you
want the compiler to restrict the year to postive
values.

One idea is to have an inline function that validates
a year:
inline Validate_Year(unsigned int year)
{
if (year < 1)
throw Your_Favorite_Exception();
return;
}

I believe this would have minimal impact since it
is declared as inline.

I don''t see any need to complicate the code by
creating a separate year class. Nor, IMHO, any
special reason to make the year a separate type.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Thomas Matthews写道:
Thomas Matthews wrote:
顺便说一句,你可以使用_unsigned_int_如果你想让编译器将年限制为正值
By the way, you could use _unsigned_int_ if you
want the compiler to restrict the year to postive
values.




他不想限制积极,他想限制为

* NON-ZERO *。略有不同。



He doesn''t want to restrict to positive, he wants to restrict to
*NON-ZERO*. Slightly different.




" JKop" < NU ** @ NULL.NULL>在留言新闻中写道:tV ***************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message news:tV*****************@news.indigo.ie...

这里有点泡菜我一直在思考。我把它简化为
发布,这里是:

Here''s a little pickle I''ve been pondering over. I''ve simplified it down for
posting, here goes:
typedef int Year;
typedef int Year;




你的复制构造函数似乎有点假。如果你不能构建一个无效的年份

对象,你怎么能得到一个无效的复制对象?


我讨厌隐式转换运算符。


是的,您将不得不定义数学运算符。想想指针/迭代器如何工作。两年减法是一种整体类型,但多年不应该含蓄地转换为数字/从数字转换。


Your copy constructor seems a bit spurious. If you can''t construct an invalid year
object, how could you ever get an invalid one to copy?

I detest implicit conversion operators.

Yes, you are going to have to define mathematic operators. Think about how pointers/iterators
work. The subtraction of two years is some integral type, but years should not otherwise implicitly
convert to/from numerics.


这篇关于价值限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆