关于公约的小问题 [英] Small Issues on Convention

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

问题描述



假设我们有一个函数返回从给定数字中减去7

的结果。你会把它写成:


(A)


int SubtractSeven(int const x)

{

返回x - 7;

}


(B)


int SubtractSeven (int x)

{

返回x - = 7;

}


(C )


int SubtractSeven(int const& x)

{

返回x - 7;

}

如果我们只是想完成任务,而不考虑代码运行的速度有多快,或者需要多少内存,那么我认为自然

的选择是A.你是否同意我的看法?


如果我们考虑效率,那么我们可能会想,为什么要创造那种暂时的?我们

应该只重用参数变量。这将使我们倾向于

B.


再次考虑效率,我们可能会想到,为什么甚至创造

那个参数变量?让我们只使用我们提供的那个(如果可能的话,可以是b $ b)。如果我们正在处理内联函数,那么这将有一些理由(我可以在这里修改

)但是如果我们正在处理

具有轮廓功能,然后我们将在引擎盖下玩指针

,这最终将远远低于我们原来的效率

例如。


所以...我的目标是为A,B和C的使用制定一些约定。


首先,我何时使用方法C?


1:当我想改变提供的变量时。


2:当我处理时使用用户定义的类型比int消耗更多内存
内存,或者如果复制构造函数的调用需要相当大的处理和动态分配内存。


例如:


1:

void SubtractSevenFromGivenVariable(int& x)

{

x - = 7;

}


2:

unsigned CountAmountOfVowels(std :: string const& str)

{

unsigned val;


//做东西


返回val;

}

您是否同意我何时使用C?

如果我们有一个如下的小POD:


struct XYCoord

{

unsigned x;

unsigned y;

};


然后我可能不会通过引用传递它,只需通过

值传递它。你在这里同意我吗?


至于A和B ......它应该是什么?


我认为A更多自然,并且是应该是什么样的方式,

但我认为我们不能否认我们看到的变量的有效重用

在B.

目前,我使用B方法,虽然我会接受

建议/建议。


我们有一个const参数并返回一个临时的吗?或者我们有一个非
const参数,并返回从中减去七的结果?


-Tomás


Let''s say we have a function that returns the result of subtracting seven
from a given number. Would you write it as:

(A)

int SubtractSeven( int const x )
{
return x - 7;
}

(B)

int SubtractSeven( int x )
{
return x -= 7;
}

(C)

int SubtractSeven( int const& x )
{
return x - 7;
}
If we just wanted to "accomplish the task", without contemplating how fast
the code will run, or how much memory it will need, then I think the natural
choice is A. Do you agree with me?

If we consider efficiency, then we may think, "Why create that temporary? We
should just re-use the parameter variable.". This would make us lean toward
B.

Considering efficiency again, we may think to ourselves, "Why even create
that parameter variable? Let''s just use the one we''re supplied with (if
possible)". This would have some rationale to it (I''m open to correction
here) if we were dealing with an inline function... but if we''re dealing
with an outline function, then we''d be playing around with pointers under
the hood, which, in the end, will be far less efficient than our original
example.

So... my aim is to draw up some conventions for the use of A, B and C.

Firstly, when do I use method C?

1: When I want to alter the supplied variable.

2: When I''m dealing with a user-defined type which consumes quite more
memory than an "int", or if the invokation of copy constructors would
involve considerable processing and dynamic allocation of memory.

Examples:

1:
void SubtractSevenFromGivenVariable(int &x)
{
x -= 7;
}

2:
unsigned CountAmountOfVowels( std::string const & str )
{
unsigned val;

//do stuff

return val;
}
Do you agree with me on when to use C?
If we had a small POD like the following:

struct XYCoord
{
unsigned x;
unsigned y;
};

then I would be probably not pass it by reference, and just pass it by
value. Do you agree with me here?

As for A and B... which shall it be?

I think that A is more natural, and is "the way things were supposed to be",
but I don''t think we can deny the efficient reuse of a variable which we see
in B.
At the moment, I use the B method, although I''d be open to
advice/suggestions.

Do we have a const parameter and return a temporary? Or do we have a non-
const parameter, and return the result of subtracting seven from it?

-Tomás

推荐答案

Tomás写道:
假设我们有一个函数返回从给定数字中减去7
的结果。你会把它写成:

int SubtractSeven(int const x)
{
返回x - 7;
}


为什么在传递值时传递const?

(B)

int SubtractSeven(int x)
{
返回x - = 7;
}


传递的参数不会被修改。

(C)

int SubtractSeven(int const& x)
{
返回x - 7;
}


传递的参数不会修改。

如果我们只是想完成任务,而不考虑代码运行的速度有多快,或者需要多少内存,那么我认为自然<选择是A.你同意我的意见吗?


编号自然选择:


int SubstractSeven(int x){

返回x - 7 ;

}

如果我们考虑效率,那么我们可能会想,为什么要创造这种临时性?我们应该只重复使用参数变量。这将使我们倾向于
B.


没有优化没有,b可能在呼叫站点复制x,并且在

中功能和在电话会议后的分配上。

再次考虑效率,我们可能会想到,为什么甚至创建那个参数变量?让我们只使用我们提供的那个(如果可能的话)。如果我们正在处理内联函数,那么这将有一些理由(我可以在这里修正)...但是如果我们用大纲函数处理
,那么我们在引擎盖下玩指针,最终效率远低于我们原来的
示例。


是什么让你觉得你会在引擎盖下玩指针?

所以......我的目标是制定一些约定使用A,B和C.首先,我何时使用方法C?

1:当我想改变提供的变量时。


但是你不是,一开始它是常数,其次是你复制品!

2:当我在处理用户定义的类型,其消耗比int更多的内存,或者如果复制构造器的调用将涉及相当大的处理和动态的存储器分配。


对不起,它被复制以修改它并无论如何返回它。但是,是的,通过

const ref,当它不是原生类型而你不需要修改它。

例如:

1:
void SubtractSevenFromGivenVariable(int& x)
{
x - = 7;
}

2:
unsigned CountAmountOfVowels(std :: string const& str)
{
unsigned val;

//做东西

返回val;
}

您是否同意我何时使用C?


见上文。

如果我们有一个如下的小POD:

struct XYCoord
{
unsigned x;
unsigned y;
};

然后我可能不会通过引用传递它,只是通过
值传递它。你在这里同意我的意见吗?


我会通过const ref传递它,更容易优化,更少维护

如果XYCoord变得更聪明,那么麻烦。

至于A和B ......它应该是什么?


两者都没有。见上文。

我认为A更自然,并且是应该是事物的方式,但是我认为我们不能否认有效的再利用我们在B中看到的变量。
目前,我使用B方法,虽然我愿意接受建议/建议。

我们有一个const参数并返回一个临时的吗?或者我们有一个非
const参数,并返回从中减去七的结果?
Let''s say we have a function that returns the result of subtracting seven
from a given number. Would you write it as:

(A)

int SubtractSeven( int const x )
{
return x - 7;
}
Why pass by const when passing by value?
(B)

int SubtractSeven( int x )
{
return x -= 7;
}
The passed parameter will not be modified.
(C)

int SubtractSeven( int const& x )
{
return x - 7;
}
The passed parameter will not be modified.
If we just wanted to "accomplish the task", without contemplating how fast
the code will run, or how much memory it will need, then I think the natural
choice is A. Do you agree with me?
No. The natural choice is:

int SubstractSeven(int x) {
return x - 7;
}
If we consider efficiency, then we may think, "Why create that temporary? We
should just re-use the parameter variable.". This would make us lean toward
B.
Nope without optimisation, b probably copies x at the call site, and in
the function and on the assignment after the call.
Considering efficiency again, we may think to ourselves, "Why even create
that parameter variable? Let''s just use the one we''re supplied with (if
possible)". This would have some rationale to it (I''m open to correction
here) if we were dealing with an inline function... but if we''re dealing
with an outline function, then we''d be playing around with pointers under
the hood, which, in the end, will be far less efficient than our original
example.
What makes you think you''d be playing around with pointers under the hood?
So... my aim is to draw up some conventions for the use of A, B and C.

Firstly, when do I use method C?

1: When I want to alter the supplied variable.
But you don''t, for a start it''s const, and secondly you make a copy!
2: When I''m dealing with a user-defined type which consumes quite more
memory than an "int", or if the invokation of copy constructors would
involve considerable processing and dynamic allocation of memory.
Sorry, it''s copied to modify it and return it anyway. But yes, pass by
const ref when it''s not a native type and you don''t need to modify it.
Examples:

1:
void SubtractSevenFromGivenVariable(int &x)
{
x -= 7;
}

2:
unsigned CountAmountOfVowels( std::string const & str )
{
unsigned val;

//do stuff

return val;
}
Do you agree with me on when to use C?
See above.
If we had a small POD like the following:

struct XYCoord
{
unsigned x;
unsigned y;
};

then I would be probably not pass it by reference, and just pass it by
value. Do you agree with me here?
I''d pass it by const ref, easier to optimise, and less maintenance
hassle if XYCoord becomes "more clever".
As for A and B... which shall it be?
Neither. See above.
I think that A is more natural, and is "the way things were supposed to be",
but I don''t think we can deny the efficient reuse of a variable which we see
in B.
At the moment, I use the B method, although I''d be open to
advice/suggestions.

Do we have a const parameter and return a temporary? Or do we have a non-
const parameter, and return the result of subtracting seven from it?




对于内置类型,你不会修改这样做:

T builtIn(T t){return t; }对于非内置类型,请执行以下操作:

T notBuiltin(const T& t){return t; }


如果你想修改参数,请执行以下操作:


T&修改(T& t){return t; }


不要担心临时是否可以重复使用,

编译器会为你完成所有这些。


就个人而言,我希望你的所有3个功能最终只能进行相同的

,只需要进行温和优化。


Ben Pope
-

我不只是一个数字。对于很多人来说,我被称为字符串...



For built in types you are not going to modify do this:
T builtIn(T t) { return t; }

For non built in types do this:
T notBuiltin(const T& t) { return t; }

If you want to modify the parameter do this:

T& modify(T& t) { return t; }

Don''t concern yourself with whether a temporary can be reused, the
compiler will do all that for you.

Personally I would expect all 3 of your functions to end up the same
with only mild optimisation.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


A:int SubtractSeven(int const x)

B:int SubtractSeven (int x)

C:int SubtractSeven(int const& x)


A的问题是,A& B具有完全相同的签名,从

编译器的角度来看。如果两件事情通常相同,那么只使用一件就更好了

。通常使用B语言。 A有点过于纯粹了。


谈到C,我认为它应该是完全禁忌甚至提及

通过const传递内置类型的可能性引用或指向

const。

A: int SubtractSeven( int const x )
B: int SubtractSeven( int x )
C: int SubtractSeven( int const& x )

Problem with A is, that A & B has exactly the same signature from
compiler point of view. If two things are the same usually it is better
to use only one. And usually B is used. A is a little bit too puristic.

Speaking about C, I think it should be total taboo even mention about
possibility to pass built in types by const reference or pointer to
const.


( A)

int SubtractSeven(int const x)
{
返回x - 7; }
为什么在通过值时传递const?
(A)

int SubtractSeven( int const x )
{
return x - 7; }
Why pass by const when passing by value?



我有一个习惯,我坚持使用const无处不在。无处不在。


无处不在。


它可能会也可能不会导致编译器优化 - 但它应该是
肯定没有不利影响。


I have a habit whereby I stick "const" everywhere that I can. Everywhere.

Everywhere.

It may or may not lead to optimisation by the compiler -- but it shall
certainly not have a detrimental effect.

(B)

int SubtractSeven(int x)
{
返回x - = 7; }
(B)

int SubtractSeven( int x )
{
return x -= 7; }



传递的参数不会被修改。



The passed parameter will not be modified.



正确。这是预期的。


Correct. This is as intended.

(C)
int SubtractSeven(int const& x)
{
返回x - 7; }
(C)

int SubtractSeven( int const& x )
{
return x - 7; }



传递的参数不会被修改。



The passed parameter will not be modified.



正确。这是预期的。


Correct. This is as intended.

如果我们只想完成任务,而不考虑如何快速运行代码,或者它需要多少记忆,那么我认为自然的选择是A.你同意我吗?
If we just wanted to "accomplish the task", without contemplating how
fast the code will run, or how much memory it will need, then I think
the natural choice is A. Do you agree with me?



没有。自然的选择是:

int SubstractSeven(int x){
返回x - 7;
}



No. The natural choice is:

int SubstractSeven(int x) {
return x - 7;
}



对你而言,是的。我自然的选择是坚持使用const。


是什么让你觉得你会在引擎盖下玩指针?


如果函数是大纲,那么机器代码将使用指向

访问内存的指示。


请不要进入A编译器的讨论。
可以自由地以任何方式实现功能,并且没有

使用指针......


For you, yes. My own natural choice was to stick in "const".

What makes you think you''d be playing around with pointers under the
hood?
If the function is outline, then the machine code will work with pointers to
access the memory.

Please don''t enter into a discussion which goes down the road of "A compiler
is free to implement features in whichever way it please, and doesn''t have
to use pointers...".

所以......我的目标是为此制定一些约定使用A,B和C.

首先,我什么时候使用方法C?

1:当我想改变提供的变量时。
So... my aim is to draw up some conventions for the use of A, B and C.

Firstly, when do I use method C?

1: When I want to alter the supplied variable.



但你不是,一开始它是常数,其次你制作副本!



But you don''t, for a start it''s const, and secondly you make a copy!




我只是在展示一个替代。我意识到我没有改变它的价值。


对于内置类型你不会修改这样做:
T builtIn(T t){return t ; }对于非内置类型,请执行以下操作:
T notBuiltin(const T& t){return t; }

如果你想修改参数,请执行以下操作:

T&修改(T& t){return t; }



I was simply showing an alternative. I realise that I don''t alter its value.

For built in types you are not going to modify do this:
T builtIn(T t) { return t; }

For non built in types do this:
T notBuiltin(const T& t) { return t; }

If you want to modify the parameter do this:

T& modify(T& t) { return t; }



是的,但是......


如果我们想通过值传递,从而不改变变量值,我们有

a选择:

(T是内在类型:)


T SubtractSeven(T t){return t - = 7 ; }


T SubtractSeven(T t){return t - 7; }


遵守我的把const放在我可以管的任何地方,然后后者将是:


T SubtractSeven(T const t) ){返回t - 7; }

因此,当我们处理内在类型时,我们应该使用:


(A)T SubtractSeven(T const t){return t - 7; }

-or-

(B)T SubtractSeven(T t){return t - = 7; }

-Tomás


Yes, but...

If we want to pass by value, thus not altering the variables value, we have
a choice of either:
(T is an intrinisc type:)

T SubtractSeven(T t) { return t -= 7; }

T SubtractSeven(T t) { return t - 7; }

Abiding by my "put const wherever I can rule", then the latter would be:

T SubtractSeven(T const t) { return t - 7; }
So when we''re dealing with intrinsic types, do we go with:

(A) T SubtractSeven(T const t) { return t - 7; }
-or-
(B) T SubtractSeven(T t) { return t -= 7; }
-Tomás


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

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