是否有一些忍者技巧可以在声明后将变量设为常量?
我知道答案是 99.99% 否,但我认为值得一试,你永远不会知道。
void SomeFunction(int a)
{
// Here some processing happens on a, for example:
a *= 50;
a %= 10;
if(example())
a = 0;
// From this point on I want to make "a" const; I don't want to allow
// any code past this comment to modify it in any way.
}
我可以用 const int b = a; 做一些类似的事情,但它实际上并不相同,并且会造成很多混乱。仅 C++0x 的解决方案是可以接受的。
编辑:另一个不太抽象的例子,就是让我问这个问题的:
void OpenFile(string path)
{
boost::to_lower(path);
// I want path to be constant now
ifstream ...
}
编辑:另一个具体的例子:重新捕获并行部分中变量的常量。
I know the answer is 99.99% no, but I figured it was worth a try, you never know.
void SomeFunction(int a)
{
// Here some processing happens on a, for example:
a *= 50;
a %= 10;
if(example())
a = 0;
// From this point on I want to make "a" const; I don't want to allow
// any code past this comment to modify it in any way.
}
I can do something somewhat similar with const int b = a;
, but it's not really the same and it creates a lot of confusion. A C++0x-only solution is acceptable.
EDIT: another less abstracted example, the one that made me ask this question:
void OpenFile(string path)
{
boost::to_lower(path);
// I want path to be constant now
ifstream ...
}
EDIT: another concrete example: Recapture const-ness on variables in a parallel section.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一种解决方案是将所有突变代码分解为 lambda 表达式。在 lambda 表达式中执行所有突变,并将结果分配给方法范围内的 const int 。例如
或者甚至
One solution would be to factor all of the mutation code into a lambda expression. Do all of the mutation in the lambda expression and assign the result out to a
const int
in the method scope. For exampleor even
您可以将生成
a
的代码移至另一个函数中:否则,没有好的方法可以在编译时执行此操作。
You could move the code to generate
a
into another function:Otherwise, there's no nice way to do this at compile time.
我曾经使用的一种模式是用 _ “隐藏”参数,因此代码变为
You can also use only const variables 并创建一个函数来计算 a 的新值(如果需要)。我更倾向于不“重用”变量,并尽可能使我的变量不可变:如果您更改某些值,则给它一个新名称。
A pattern I used to use is to "hide" the argument with an _, so the code becomes
You could also use only const variables and make a function to compute the new value of a, if necessary. I tend more en more to not "reuse" variables en make as much as possible my variables immutable : if you change the value of something , then give it a new name.
为什么不将代码重构为两个单独的函数。一个返回修改后的
a
,另一个则处理该值(不更改它)。您也可以将您的对象包裹在持有者类对象周围并使用此持有者。
您的示例有一个简单的修复方法:重构。
Why not refactor your code in to two separate functions. One that returns a modified
a
and another that works on this value (without ever changing it).You could possibly wrap your object too around a holder class object and work with this holder.
Your example has an easy fix: Refactoring.
如果您只是想避免使用另一个名称,这可能是一种方法。我建议你在使用这个之前三思而后行。
this might be one way to do it, if you are just trying to avoid another name. i suggest you think twice before using this.
我实际上并不建议这样做,但您可以使用创意变量阴影来模拟您想要的东西:
I don't actually suggest doing this, but you could use creative variable shadowing to simulate something like what you want:
答案非常可靠,但老实说,我真的想不出使用它的好情况。但是,如果您想预先计算一个常数,这基本上就是您正在做的事情,您有几种主要方法可以做这。
首先我们可以进行以下操作。因此编译器会简单地为我们设置 CompileA#,在本例中它是 50、100 和 150。
现在除了这个之外,还有很多方法可以处理这个问题。我喜欢这个建议,就像其他人提到的那样。
但另一种方式可能是......
或者甚至......
Answers were pretty solid, but honestly I can't really think of a GOOD situation to use this in. However in the event you want to Pre-Calculate a constant which is basically what you are doing you have a few main ways You can do this.
First we can do the following. So the compiler will simply set CompileA# for us in this case it's 50, 100, and 150.
Now anything beyond that there's so many ways you can handle this. I liked the suggestion as someone else had mentioned of doing.
But another way could be...
Or even..
当然,在 C++ 中没有办法使用相同的变量名来做到这一点。
Sure, there is no way to do it using the same variable name in C++.