样式:更喜欢括号到'='进行初始化? [英] Style: preferring parentheses to '=' for initialization?

查看:47
本文介绍了样式:更喜欢括号到'='进行初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现自己越来越倾向于使用

这样的做法,以避免使用

初始化的形式。 ''=''符号,理由是它可能很容易被错误地分配给错误。我喜欢避免错误。

我更喜欢频繁的,微妙的强化(对于我自己和我的b
同事),分配和初始化是

完全不同的操作。


但是,有对立点。首先,我仍然陷入旧习惯

- 我不认为这是一个主要问题,因为最糟糕的是,b
发生的事情是我有点不一致(无论如何都是这样的情况,

并且可能更大程度上,除非一个人在另一个方面具有宗教信仰并始终使用''='')。更值得关注的是

可读性。由于旧的习惯,在某些情况下我发现代码稍微不那么可读在这种做法下,虽然我希望随着时间的推移这会减少

。不过,我也要关心我的同事。


所以,你们都有什么想法?还有其他人这样做吗?您对可读性问题感觉如何?b $ b?任何其他考虑因素我都没有确认吗?


卢克

Lately I find myself increasingly preferring the practice of going
ever-so-slightly out of my way to avoid the use of the form of
initialization which uses the ''='' symbol, on the grounds that it can be
mistaken too easily for assignment. I like the avoidance of mistakes.
I like even more the frequent, subtle reinforcement (for myself and my
colleagues) of the point that assignment and initialization are
entirely different operations.

However, there are counterpoints. First, I still fall into old habits
-- I don''t consider this a major problem, since the worst that can
happen is that I am a little inconsistent (which is the case anyway,
and probably to greater degree, unless one is religious in the other
direction and always uses ''=''). Of more significant concern is
readability. Due to old habits, in some cases I find the code slightly
less readable under this practice, though I expect this to diminish
with time. I have to be concerned with my colleagues as well, though.

So, what do you all think? Does anyone else do this? How do you feel
about the readability issue? Any other considerations I''ve failed to
identify?

Luke

推荐答案

Luke Meyers写道:
Luke Meyers wrote:
最近我发现自己越来越喜欢这样做的做法,因为我不顾一切地避免使用
初始化使用''=''符号,理由是它可能很容易被误认为是作业。我喜欢避免错误。
我更喜欢频繁,微妙的强化(对我自己和我的同事),分配和初始化是完全不同的操作。

然而,有对立面。首先,我仍然陷入旧习惯中 - 我不认为这是一个主要问题,因为可能发生的最糟糕的事情是我有点不一致(无论如何,情况如此,<并且可能更大程度上,除非一个人在另一个方向上具有宗教信仰并且总是使用'​​'='')。更值得关注的是可读性。由于旧的习惯,在某些情况下,我发现在这种做法下代码略显可读,但我希望随着时间的推移,这会减少。不过,我也要关心我的同事。

那么,你们都有什么想法?还有其他人这样做吗?您对可读性问题有何看法?我未能确定的任何其他考虑事项?

卢克
Lately I find myself increasingly preferring the practice of going
ever-so-slightly out of my way to avoid the use of the form of
initialization which uses the ''='' symbol, on the grounds that it can be
mistaken too easily for assignment. I like the avoidance of mistakes.
I like even more the frequent, subtle reinforcement (for myself and my
colleagues) of the point that assignment and initialization are
entirely different operations.

However, there are counterpoints. First, I still fall into old habits
-- I don''t consider this a major problem, since the worst that can
happen is that I am a little inconsistent (which is the case anyway,
and probably to greater degree, unless one is religious in the other
direction and always uses ''=''). Of more significant concern is
readability. Due to old habits, in some cases I find the code slightly
less readable under this practice, though I expect this to diminish
with time. I have to be concerned with my colleagues as well, though.

So, what do you all think? Does anyone else do this? How do you feel
about the readability issue? Any other considerations I''ve failed to
identify?

Luke




您需要注意的一个问题(如果你还没有)在尝试使用

一个未命名的临时对象来构造一个对象时可能遇到的奇怪的问题。考虑以下示例:


A类

{

public:

显式A(int ){}

};


B级

{

公开:

显式B(const A&){}

int f(){return 42; }

};

int main()

{

int x = 3;

B b(A(x));


bf(); //哎呀! b不是你预期的变量!

}

感兴趣的行是:

B b(A(x));


你会认为这会创建一个未命名的A型临时,并将它传递给B'的构造函数。然而,事实证明,当声明一个

函数时,你可以在参数名称周围添加括号。

所以C ++实际上会将这一行解释为:

B b(A x);


也就是说,一个名为b的函数的声明返回一个对象为

类型B和取一个A类参数。特别麻烦的是b / b $ b部分是没有语法问题,所以你不会得到错误

直到你真正尝试使用b。初始化的''=''

语法可以避免这个问题。例如:

B b = B(A(x));


另一种解决方案,如果您仍然希望将''='留下

初始化,是引入另一组括号:

B b((A(x)));


- -

Alan Johnson



One issue you need it be aware of (if you aren''t already) is the weird
parsing issue you can run into when trying to construct an object with
an unnamed temporary. Consider the following example:

class A
{
public:
explicit A(int) {}
} ;

class B
{
public:
explicit B(const A &) {}
int f() { return 42 ; }
} ;
int main()
{
int x = 3 ;
B b(A(x)) ;

b.f() ; // Oops! b isn''t a variable like you expected!
}
The line of interest is:
B b(A(x)) ;

You''d think this would create an unnamed temporary of type A, and pass
it to B''s constructor. However, it turns out that when declaring a
function, you are allowed to put parenthesis around the argument names.
So C++ will actually interpret this line as:
B b(A x) ;

That is, the declaration of a function named b that returns an object of
type B and takes one parameter of type A. The particularly troublesome
part is that there is no syntax problem there, so you don''t get an error
until you actually try to use b. This problem is avoided with the ''=''
syntax for initialization. Example:
B b = B(A(x)) ;

Another solution, if you still prefer to leave ''='' out of your
initialization, is to introduce another set of parenthesis:
B b((A(x))) ;

--
Alan Johnson


Alan Johnson< al **** @ no.spam.stanford.edu>写道:
Alan Johnson <al****@no.spam.stanford.edu> wrote:
另一个解决方案,如果你仍然希望将''=''从初始化中删除,那就是引入另一组括号:
B b((A(x)));
Another solution, if you still prefer to leave ''='' out of your
initialization, is to introduce another set of parenthesis:
B b((A(x))) ;




呃。为什么不用临时中间词明确表示:


临时(x);

B b(临时);

当然,它需要两行而不是一行,但它很容易阅读并且

完全明确。让编译器担心暂时优化




Ugh. Why not just make it explicit with a temporary intermediate:

A temp(x);
B b(temp);

Sure, it takes two lines instead of one, but it''s easy to read and
completely unambiguous. Let the compiler worry about optimizing away the
temporary.


Roy Smith写道:
Roy Smith wrote:
Alan Johnson< al ****@no.spam.stanford.edu>写道:
Alan Johnson <al****@no.spam.stanford.edu> wrote:
另一个解决方案,如果你仍然希望将''=''从初始化中删除,那就是引入另一组括号:
B b((A(x)));
Another solution, if you still prefer to leave ''='' out of your
initialization, is to introduce another set of parenthesis:
B b((A(x))) ;



呃。为什么不用临时中间词来表达它:

一个临时(x);
B b(临时);

当然,它需要两行代替一个,但它很容易阅读和
完全明确。让编译器担心优化而不是暂时的。



Ugh. Why not just make it explicit with a temporary intermediate:

A temp(x);
B b(temp);

Sure, it takes two lines instead of one, but it''s easy to read and
completely unambiguous. Let the compiler worry about optimizing away
the temporary.



这不是一个临时的,至少是编译器所关注的AFA。它是一个成熟的对象(有一个非常常见的名字,请注意),将

引入与意图(''b'')对象相同的范围内。因此,在尝试通过称之为临时来掩盖它时,没有任何感觉。这个术语具有非常具体和明确的C ++含义。


V

-

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

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



It''s not a temporary, at least AFA the compiler is concerned. It is
a full-fledged object (with a very common name, mind you), introduced
into the same scope as the intented (''b'') object. So, there is no
sense in trying to mask that by calling it "temporary". The term has
a very specific and well-defined meaning in C++.

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


这篇关于样式:更喜欢括号到'='进行初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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