扔掉.vs。不可扔? [英] throwable .vs. non throwable?

查看:86
本文介绍了扔掉.vs。不可扔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于转换类型的模板:


模板< typename T,typename FT StrmConvert(const F from)

{

std :: stringstream temp;

temp<< from;

T to = T();

temp> to;

返回;

}


模板< typename Fstd :: string StrmConvert(const F from)

{

返回StrmConvert< std :: string> (来自);

}


正如你所看到的,它并没有扔掉。如果无法进行转换,那么它只需返回一个默认的构造类型,这对于我使用它的大多数情况都是好的。但是,可能存在我想抛出错误的情况。

我知道我可以简单地复制这个模板并给它一个新名称,例如

StrmConvertThrow但是我不喜欢那个解决方案。我想要的是什么?b $ b能够做到(这可能是不可能的)是这样的:


int i = 10;

std :: string x;


x = StrmConvert(i):throwable;


等。我不认为这是对的,好吧,好吧,我知道这是不对的。我知道函数有一个throw(int)类型的关键字。
我试着像这样重复StrmConvert $

模板< typename T,typename FT StrmConvert(const F from)throw(

int)


但是编译时错误。


console5.cpp(17):警告C4290:忽略C ++异常规范,除了

表示函数不是__declspec(nothrow)

console5.cpp(24):错误C2382:''StrmConvert'':重新定义;不同的

异常规格

console5.cpp(3):看到''StrmConvert'的声明'


显然C ++没有通过抛出

来区分功能签名。虽然我似乎记得有一些关于有一个新的,和一个新的扔掉的东西。


我是在咆哮错误的树,还是有办法做我想做的事情?

I have a template I use for converting types:

template<typename T, typename F T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}

template<typename Fstd::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}

As you can see, it doesn''t throw. If the conversion can not take place,
then it simply returns a default constructed type, which is fine for most
cases I use it. However, there may be a case where I want to throw on error.
I know I could simply copy this template and give it a new name, such as
StrmConvertThrow but I don''t like that solution. What I would like to be
able to do (which may not be possible) is something like:

int i = 10;
std::string x;

x = StrmConvert( i ):throwable;

or such. I don''t think that''s right, well, okay, I know it''s not right. I
do know that there is a throw( int ) type keyword for functions. I tried to
duplicate StrmConvert like this:

template<typename T, typename F T StrmConvert( const F from ) throw(
int )

but got compile time errors.

console5.cpp(17) : warning C4290: C++ exception specification ignored except
to indicate a function is not __declspec(nothrow)
console5.cpp(24) : error C2382: ''StrmConvert'' : redefinition; different
exception specifications
console5.cpp(3) : see declaration of ''StrmConvert''

so apparently C++ doesn''t distinguish between function signatures by throw
or not. Although I seem to recall something about there being a new, and a
new throwable.

Am I barking up the wrong tree, or is there a way to do what I want?

推荐答案

2007年7月23日星期一18:39:27 -0700,Jim Langston写道:
On Mon, 23 Jul 2007 18:39:27 -0700, "Jim Langston" wrote:

>我有一个用于转换类型的模板:

模板< typename T,typename FT StrmConvert(const F from )
{b / b $ b std :: stringstream temp;

temp<< from;

T to = T();

temp> to;

返回;
}

模板< typename Fstd :: string StrmConvert(const F from)
{

返回StrmConvert< std :: string>(from);
}

正如你所看到的,它并没有扔掉。
>I have a template I use for converting types:

template<typename T, typename F T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}

template<typename Fstd::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}

As you can see, it doesn''t throw.



代码中的每个语句都可能抛出异常。

Each statement in your code may throw an exception.


>如果转换不能采取地方,
然后它只返回一个默认的构造类型,
>If the conversion can not take place,
then it simply returns a default constructed type,



也许,也许不是。对于任何类型的T,你都不能预见到这一点。

Maybe, maybe not. You cannot forsee this for any type T.


>这对我使用它的大多数情况都没问题。但是,可能存在我想抛出错误的情况。
我知道我可以简单地复制这个模板并给它一个新名称,例如
StrmConvertThrow但我不喜欢那个解决方案。我希望能做什么(可能不可能)是这样的:
>which is fine for most
cases I use it. However, there may be a case where I want to throw on error.
I know I could simply copy this template and give it a new name, such as
StrmConvertThrow but I don''t like that solution. What I would like to be
able to do (which may not be possible) is something like:



你误解了C ++中的异常规范。一般来说,

模板和异常规格都不会混合。

-

Roland Pibinger

" ;最好的软件简单,优雅,充满戏剧性。 - Grady Booch

You misunderstood exception specifications in C++. In general,
templates and exception specifications don''t blend.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch


7月24日凌晨3:39,Jim Langston < tazmas ... @ rocketmail.comwrote:
On Jul 24, 3:39 am, "Jim Langston" <tazmas...@rocketmail.comwrote:

我有一个用于转换类型的模板:
I have a template I use for converting types:


模板< typename T,typename FT StrmConvert(const F from)

{

std :: stringstream temp;

temp<< from;

T to = T();

temp> to;

返回;

}
template<typename T, typename F T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}


template< typename Fstd :: string StrmConvert(const F from)

{

返回StrmConvert< std :: string>(from);

}
template<typename Fstd::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}


如你所见,它没有''扔。
As you can see, it doesn''t throw.



我没有看到。在我的系统上,它肯定会引发某些

案件。 (如果你没有运用这段代码足以看到

bad_alloc异常,那么你还没有对它进行过足够的测试。)

I don''t see that. On my system, it certainly throws in certain
cases. (If you''ve not exercised this code enough to see a
bad_alloc exception, then you''ve not tested it enough.)


如果无法进行转换,

那么它只返回一个默认的构造类型,这对我使用它的大多数情况都很好。但是,可能存在我想抛出错误的情况。

我知道我可以简单地复制这个模板并给它一个新名称,例如

StrmConvertThrow但是我不喜欢那个解决方案。我想做什么

能够做到(这可能是不可能的)类似于:
If the conversion can not take place,
then it simply returns a default constructed type, which is fine for most
cases I use it. However, there may be a case where I want to throw on error.
I know I could simply copy this template and give it a new name, such as
StrmConvertThrow but I don''t like that solution. What I would like to be
able to do (which may not be possible) is something like:


int i = 10;

std :: string x;
int i = 10;
std::string x;


x = StrmConvert(i):throwable;
x = StrmConvert( i ):throwable;


等。
or such.



没有办法。一个函数是否可以抛出是不是在重载分辨率中考虑的
,并且你无法指定你想要的版本没有'b $ b'抛出,或者说
确实如此。


显而易见的解决方案是将抛出行为设为

默认,并写入类似于:


T myT;

尝试{

myT = StrmConvert(i);

}赶上(...){

}


如果你不在乎(这应该是非常罕见的
在大多数应用程序中

There''s no way. Whether a function can throw or not is not
considered in overload resolution, and there''s no way for you to
specify that you want a version that doesn''t throw, or that
does.

The obvious solution is to make the throwing behavior the
default, and to write something like:

T myT ;
try {
myT = StrmConvert( i ) ;
} catch ( ... ) {
}

in the case where you don''t care (which should be extremely rare
in most applications).


我不认为这是对的,好吧,好吧,我知道这是不对的。我知道函数有一个throw(int)类型的关键字。

I don''t think that''s right, well, okay, I know it''s not right. I
do know that there is a throw( int ) type keyword for functions.



这是一个异常规范。

按当前语言合同编程的独特例子 - 它表示一个

合约,说这个函数不会抛出任何东西

,否则,如果该函数违反了合同,它将终止该程序的
。 (类似于assert(),真的。)

That''s an exception specification. The unique example of
programming by contract in the current language---it expresses a
contract, saying that this function will not throw anything
else, and if the function violates the contract, it terminates
the program. (Sort of like assert(), really.)


我试图像这样复制StrmConvert:
I tried to duplicate StrmConvert like this:


模板< typename T,typename FT StrmConvert(const F from)throw(

int)
template<typename T, typename F T StrmConvert( const F from ) throw(
int )


但得到编译时错误。
but got compile time errors.


console5.cpp(17):警告C4290:忽略C ++异常规范,除非

表示函数不是__declspec (nothrow)

console5.cpp(24):错误C2382:''StrmConvert'':重新定义;不同的

异常规格

console5.cpp(3):参见''StrmConvert'的声明'
console5.cpp(17) : warning C4290: C++ exception specification ignored except
to indicate a function is not __declspec(nothrow)
console5.cpp(24) : error C2382: ''StrmConvert'' : redefinition; different
exception specifications
console5.cpp(3) : see declaration of ''StrmConvert''


显然C ++并没有区分函数

签名是否抛出。
so apparently C++ doesn''t distinguish between function
signatures by throw or not.



异常规范不是函数

签名的一部分。只不过是身体中的一个断言。

The exception specification is not part of the function
signature. No more than an assert in the body would be.


虽然我似乎记得有一些新的,

和一个新的抛出。
Although I seem to recall something about there being a new,
and a new throwable.



这是一个完全不同的问题。通常,new会通过抛出std :: bad_alloc来通知

内存不足。如果用户想要
获得一个空指针,他可以使用带有

no_throw附加参数的新位置,而new将返回null,而不是
如果没有足够的内存,
比抛出。

That''s a completely different issue. Normally, new notifies the
lack of memory by throwing std::bad_alloc. If the user wants to
get a null pointer instead, he can use a placement new with a
no_throw additional parameter, and new will return null, rather
than throwing, if there is not enough memory.


我是在咆哮错误的树,还是有办法做我所做的事情>
想要?
Am I barking up the wrong tree, or is there a way to do what I
want?



我的立即反应是避免这种功能开始。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34

My immediate reaction would be to avoid this sort of function to
begin with.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


" James Kanze" < ja ********* @ gmail.comwrote in message

news:11 ********************* *@m3g2000hsh.googlegro ups.com ...

7月24日凌晨3:39,Jim Langston < tazmas ... @ rocketmail.comwrote:

[SNIP好回复]
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@m3g2000hsh.googlegro ups.com...
On Jul 24, 3:39 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
[SNIP good replies]

虽然我似乎回忆起有关新的,
和新的扔掉的东西。
Although I seem to recall something about there being a new,
and a new throwable.



这是一个完全不同的问题。通常,new会通过抛出std :: bad_alloc来通知

内存不足。如果用户想要
获得一个空指针,他可以使用带有

no_throw附加参数的新位置,而new将返回null,而不是
如果没有足够的内存,
比投掷。


That''s a completely different issue. Normally, new notifies the
lack of memory by throwing std::bad_alloc. If the user wants to
get a null pointer instead, he can use a placement new with a
no_throw additional parameter, and new will return null, rather
than throwing, if there is not enough memory.



我想我会看看no_throw附加参数,并尝试将
添加到StrmConvert。如果这就是放置新的方式,那么

也许我应该效仿它。虽然它很可能是一些枚举或定义,但是我想用相对标准的东西。

I think I''ll take a look at the no_throw additional parameter, and attempt
to add it to StrmConvert. If that''s the way placement new does it, then
maybe I should emulate it. Although it''s most likely some enum or define,
I''d like to use something relatively standard.


我是在咆哮错误的树,还是有办法做我想要的b $ b想要的东西?
Am I barking up the wrong tree, or is there a way to do what I
want?



我的立即反应是避免这种功能开始。


My immediate reaction would be to avoid this sort of function to
begin with.



好​​吧,这段代码是为了简单的输出而设计的,而不必在我的主线上构建字符串流。

我已经开始使用它来通过套接字进行用户输入,并且有很多

次允许默认构造值是完全可以接受的。在

的事实上,还没有一个案例会导致我的程序崩溃

如果它不是。我解析了字段并进行了任何粗略的检查

我将它们传递给StrmConvert。这实际上是我想知道的罕见场合

如果它没有正确转换(事实上,还没有一个案例,但

我为未来而建设。


对于我将来可能会处理的其他代码,我可能希望它抛出,这是

为什么我正在寻找最好的方法。

Well, this code was orignally designed for simple ouput without having to
build stringstream in my mainline.

I''ve since gone to using it for user input via sockets and there are many
times it is perfectly acceptable to allow default constructed values. In
fact, there hasn''t been a case yet where it would cause my program to crash
if it wasn''t. I parse out the fields and do any rudementary checking before
I pass them to StrmConvert. It is actually the rare occasion I want to know
if it didn''t convert correctly (in fact, there hasn''t been a case yet, but
I''m building for the future).

For other code I may work on in the future, I may want it to throw, which is
why I''m looking at the best way to do it.


这篇关于扔掉.vs。不可扔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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