typedef道德 [英] typedef Morality

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

问题描述




我最近开始使用typedef,如果他们曾经被认为是邪恶的话,我很好奇。


特别是,我正在考虑在
a文件中使用typedef但在另一个包含文件中定义的情况。阅读

代码的人可能需要追踪原始定义以理解代码的含义,从而降低了可读性。


我在C ++ FAQ中找不到任何谴责typedef的东西,所以

我猜它们不被认为是坏形式。有人对

主题有什么想法吗?


谢谢,

cpp

Hi,

I recently started using typedefs and am curious if they are ever
considered evil.

In particular, I''m thinking of a situation where a typedef is used in
a file but defined in another, included file. Someone reading the
code might have to track down the original definition to make sense of
the code, and thus it reduces readability.

I didn''t find anything int the C++ FAQ denouncing typedefs, though, so
I''m guessing they''re not considered bad form. Anyone have thoughts on
the subject?

Thanks,
cpp

推荐答案

cppaddict写道:
cppaddict wrote:
我最近开始使用typedef,如果它们被视为邪恶,我很好奇。


他们很好。代码应遵循不要重复自己。对于行为(方法中的{}内部的东西),这个规则是最重要的。将类型视为

结构,不重复不太重要,但每一点点

都有帮助。例如:


typedef std :: map< string,string> params_t;


bool

产品(params_t& testCase)

{

double num( strtod(testCase [" Input1"]。c_str(),NULL));

double den(strtod(testCase [" Input2"]。c_str(),NULL));

double quo(strtod(testCase [" Product()"] .c_str(),NULL));


return num * den == quo; < br $>
}


我要弃去通常说双倍的行

(strtod(testCase ["产品] ()"] .c_str(),NULL));单个重复使用

功能。当我这样做时,typedef params_t将出现在它的参数中。这个

让我们只在一个地方升级params_t,而不是写所有

std :: map< etc etc>各地。


此外,模板实例化到一个类的确切点往往是
与实例化点所见的类型相结合的模板。

重复std :: map< etc etc>到处都可以在每个实例之间产生细微的差异,因此使用typedef进行实例化会降低这种风险,并且在标头中实例化可以减少更多。 (但不能消除它,因为用户可以在任何

订单中#include他们的标题。)

特别是,我我想的是在一个文件中使用typedef但在另一个包含文件中定义的情况。有人阅读
代码可能需要追踪原始定义以理解代码,从而降低了可读性。
I recently started using typedefs and am curious if they are ever
considered evil.
They are good. Code should follow Don''t Repeat Yourself. That rule''s most
important for behavior (the stuff inside {} on methods). Consider types as
structure, which is less important to not duplicate, but every little bit
helps. For example:

typedef std::map<string, string> params_t;

bool
Product(params_t & testCase)
{
double num (strtod(testCase["Input1"].c_str(), NULL));
double den (strtod(testCase["Input2"].c_str(), NULL));
double quo (strtod(testCase["Product()" ].c_str(), NULL));

return num * den == quo;
}

I''m about to fold the lines that generally say double quo
(strtod(testCase["Product()" ].c_str(), NULL)); into a single reuseful
function. When I do, the typedef params_t will appear in its arguments. This
lets us upgrade params_t in only one place, and not write all the
std::map<etc etc> all over the place.

Further, the exact point where a template instantiates into a class tends to
conjoin that template with the types seen at the instantiation point.
Repeating std::map<etc etc> all over the place could generate subtle
differences between each instance, so instantiating with a typedef reduces
this risk, and instantiating in a header reduces it more. (It can''t
eliminate it though, because users could #include their headers in any
order.)
In particular, I''m thinking of a situation where a typedef is used in
a file but defined in another, included file. Someone reading the
code might have to track down the original definition to make sense of
the code, and thus it reduces readability.




那里有许多方法可以通过扩展模块来降低可读性,因此,他们的声明远远超出了他们的定义,无论是身体上还是认知上都很远。为了提高可读性,可以用简短的方法编写简单的代码,

并尽可能缩小范围。如果另一个模块

某处需要std :: map< string,string>,我会_not_将params_t放入

这个模块可以看到的标题。我会在

中用另一个名称写另一个typedef。


在他们的意图总是有助于提高可读性之后命名。 params_t

打算将参数存储到方法中,作为字符串的映射。另一个模块

会以不同的方式命名他们的typedef。


为了帮助导航这个短距离,使用VC ++,单击

params_t然后点击< F12> ;.


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces


cppaddict写道:
cppaddict wrote:
我最近开始使用typedef并且好奇他们是否被认为是邪恶的。

特别是,我在想在文件中使用typedef但在另一个包含文件中定义的情况。有人阅读
代码可能需要追踪原始定义以理解代码,从而降低了可读性。

我没有找到任何内容C ++ FAQ谴责typedef,所以
我猜他们不被认为是坏形式。有人对这个主题有什么想法吗?
I recently started using typedefs and am curious if they are ever
considered evil.

In particular, I''m thinking of a situation where a typedef is used in
a file but defined in another, included file. Someone reading the
code might have to track down the original definition to make sense of
the code, and thus it reduces readability.

I didn''t find anything int the C++ FAQ denouncing typedefs, though, so
I''m guessing they''re not considered bad form. Anyone have thoughts on
the subject?




Typedef在C ++中和英文缩写一样重要。他们

也增加了很多混乱。但是,最重要的

部分(我相信)与标准

库中某些别名的使用有关,我主要是指像''value_type'这样的typedef ',

''difference_type'',''iterator_category''用于迭代器,例如。

没有它们,模板函数之间就没有干净的界面
库中的
和其他人设计的自定义迭代器。相同的

代表容器的'value_type'',''result_type''代表函数和

等等。


Victor



Typedefs are as important in C++ as abbreviations in English. They
add about as much confusion, as well. However, the most important
part (I believe) relates to the use of certain aliases in the Standard
Library, and I mostly refer to such typedefs like ''value_type'',
''difference_type'', ''iterator_category'' for iterators, for example.
Without them there would be no clean interface between template functions
in the Library and custom iterators designed by other people. The same
goes for ''value_type'' for containers, ''result_type'' for functions and
so on.

Victor


Philip,


感谢您的回复。我想澄清一点,如果

可能......
Philip,

Thanks for your reply. I''d like clarification on one point, if
possible...
如果另一个模块
某处需要std :: map<字符串,字符串>,我会_not_将params_t放入其模块可以看到的标题中。我会在
那个模块中写另一个typedef,名字不同。
If another module
somewhere needs a std::map<string, string>, I would _not_ put params_t into
a header that both its modules can see. I would write another typedef in
that module, with a different name.




甚至是同一个名字。我只是尝试重新定义一个typedef,当

已经定义了同一个,并且编译时没有错误。

这样做是解决我最初关注的一种方法。或者那是

邪恶?你觉得怎么样?


谢谢,

cpp



Or even the same name. I just tried redefining a typedef when the
same one had already been defined, and it compiled without errors.
Doing this would be one way to resolve my initial concern. Or is that
evil? what do you think?

Thanks,
cpp


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

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