有效使用演员表 [英] valid use of cast

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

问题描述

我有以下内容:

1:模板< class Sumtype,class Averagetype>

2:Averagetype Average(Sumtype * p_array,Averagetype p_count)

3:{

4:int index ;

5:Sumtype sum = 0;

6:for(index = 0; p_count index; index ++)

7:sum + = p_array [index];

8:返回(Averagetype)sum / p_count;

9:}


它是'从我目前正在阅读的一本书。

我很想知道标准是什么意思

关于对平均类型的演员?


-

conrad

解决方案

conrad写道:


我有以下内容:

1:template< class Sumtype,class Averagetype>

2:Averagetype Average(Sumtype * p_array,Averagetype p_count)

3:{

4:int index ;

5:Sumtype sum = 0;

6:for(index = 0; p_count index; index ++)

7:sum + = p_array [index];

8:返回(Averagetype)sum / p_count;

9:}


它是'从我正在阅读的一本书中读到。

我很想知道标准是什么意思

关于对平均类型的演员?



没什么。标准中没有提到''Averagetype''。

严肃的说明,你期望标准说什么?

C风格的演员阵容是其中的一部分对于这种语言,他们通常会结束

为static_cast或const_cast或reinterpret_cast或某些

两种组合的组合,或者有时是其他的东西
$ b没有标准C ++强制转换支持的$ b。在你的情况下,

演员可能是static_cast。


V

-

请删除资本''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问


< blockquote> conrad写道:


1:template< class Sumtype,class Averagetype>

2:Averagetype Average(Sumtype * p_array,Averagetype p_count)

3:{



....


8:return(Averagetype)sum / p_count;

9:}


这是我正在阅读的一本书。

我很想知道标准说什么

关于演员表到平均型?



它调用Averagetype :: Averagetype(Sumtype const& x),它必须存在(在输入类型变体中为
)。 br />

接下来,它可能是用构造函数表示法写的:


Averagetype(sum)


或者作为elaborate_cast:


static_cast< Averagetype>(总和)


问题:哪种风格更好?

-

Phlip




Phlip< ph ****** @ yahoo.com在留言中写道...


>

它调用Averagetype :: Averagetype(Sumtype const& x),必须存在(在输入类型变体中为
)。


接下来,它可能是用构造符表示法写的:


Averagetype(sum)


或者作为elaborate_cast:


static_cast< Averagetype>(总和)


问题:哪种风格更好?



C ++演员。它是如此丑陋,你不能忽视它!

编译时更好的错误/警告。


此外,它使搜索更容易一个编辑。

(搜索(非全字):" _cast<",会找到所有。请尝试使用

C风格的演员表!)


我使用''constructor-notation'来表示我所知道的简单事情(相对而言)

safe:

for(int i(0); size_t(i)< somevector.size(); ++ i){

//某些需要int的操作。

}

....''size_t(i)''关闭编译器警告。


-

Bob R

POVrookie


I have the following:
1: template< class Sumtype, class Averagetype >
2: Averagetype Average( Sumtype* p_array, Averagetype p_count )
3: {
4: int index;
5: Sumtype sum = 0;
6: for( index = 0; p_count index; index++ )
7: sum += p_array[index];
8: return (Averagetype)sum / p_count;
9: }

It''s from a book that I am currently reading.
I''m curious to know what the standard says
about the cast to Averagetype?

--
conrad

解决方案

conrad wrote:

I have the following:
1: template< class Sumtype, class Averagetype >
2: Averagetype Average( Sumtype* p_array, Averagetype p_count )
3: {
4: int index;
5: Sumtype sum = 0;
6: for( index = 0; p_count index; index++ )
7: sum += p_array[index];
8: return (Averagetype)sum / p_count;
9: }

It''s from a book that I am currently reading.
I''m curious to know what the standard says
about the cast to Averagetype?

Nothing. There is no mention of ''Averagetype'' in the Standard.
On the serious note, what do you expect the Standard to say?
C-style casts are part of the language, they usually end up
being static_cast or const_cast or reinterpret_cast or some
kind of combination of both, or, sometimes, something else
that none of standard C++ casts support. In your case the
cast is probably static_cast.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


conrad wrote:

1: template< class Sumtype, class Averagetype >
2: Averagetype Average( Sumtype* p_array, Averagetype p_count )
3: {

....

8: return (Averagetype)sum / p_count;
9: }

It''s from a book that I am currently reading.
I''m curious to know what the standard says
about the cast to Averagetype?

It calls Averagetype::Averagetype(Sumtype const & x), which must exist (to
within input type variations).

Next, it could have been written with constructor notation:

Averagetype(sum)

Or as an elaborate_cast:

static_cast<Averagetype>(sum)

Question: Which is better style?

--
Phlip



Phlip <ph******@yahoo.comwrote in message...

>
It calls Averagetype::Averagetype(Sumtype const & x), which must exist (to
within input type variations).

Next, it could have been written with constructor notation:

Averagetype(sum)

Or as an elaborate_cast:

static_cast<Averagetype>(sum)

Question: Which is better style?

The C++ cast. It is so ugly, you can''t ignore it!
Better error/warnings when compiling.

Also, it makes it easy to search for with an editor.
( search (non-whole word): "_cast<", will find ''em all. Try that with a
C-style cast! )

I use the ''constructor-notation'' for simple things I know are (relatively)
safe:

for( int i(0); size_t( i ) < somevector.size(); ++i ){
// some op that needs an int.
}
.... the ''size_t( i )'' shuts-up the compiler warning.

--
Bob R
POVrookie


这篇关于有效使用演员表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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