Why not use __if_exists with local variables?

痞子三分冷 提交于 2020-07-23 04:25:44

问题


The MSDN documentation for the Microsoft-specific __if_exists statement says the following (emphasis added):

Apply the __if_exists statement to identifiers both inside or outside a class. Do not apply the __if_exists statement to local variables.

Unfortunately there is no explanation for why you should not apply this to local variables. It compiles fine and has the expected effect, so I'm wondering if anyone knows why they say not to do this. Is it a correctness issue, or a maintainability issue, or something else?

I realize that this is a Microsoft-specific feature and not portable, but let's assume for argument's sake that there's a good reason to use it.

EDIT: Some folks are curious why I'm doing this, so here's an explanation. I realize this is a dirty hack, so unless you have a good suggestion for a better way to do it, please don't bother pointing out that it's gross. It's the least-gross alternative we were able to find given the large size of the code base.

We have a large body of legacy code (millions of lines) that uses the Microsoft-specific __FUNCTION__ macro as part of an error logging package. A significant fraction of that code is now wrapped inside lambda functions so that we can catch structured exceptions (with __try/__except) and still use unwindable objects. Inside those lambda functions, __FUNCTION__ evaluates to something useless like `anonymous-namespace'::<lambda23>::operator(), which is not useful for anything. Our workaround for this is to define new __FUNCTION__-like macro which checks for the existence of an alternate local variable with the enclosing function name, using __if_exists. Due to how the macros work, we can easily switch to the new __FUNCTION__ substitute and easily define the alternate name variable without changing tons of code, so it's a reasonably clean solution given the limitations. That is, of course, assuming that it's valid to use __if_exists this way.

As I said above, I know it's a dirty hack, so please don't tell me how ugly it is unless you have good ideas on how to do it better.


回答1:


I don't know for sure, but one guess is a local variable might be optimized away by compiler, and maybe not of course, which renders __if_exists test unrelieable.

And I also don't see the reason to do this for a local variable, you are in that specific scope, you know everything, why you want to test if a local variable exist?




回答2:


__if_exists is a dirty old hack inside Visual C++, with severe implementation limitations as it was only intended for ATL.

Local variables are special because you can have two local variables with the same name:

void foo()
{ 
  int i = 1;
  {
    int i = 2;
  }
}

This means there's a more complicated datastructure inside the compiler to track them. __if_exists has to do a name lookup, which may not be correct for some types of nested scopes like this.

Another historical case is that in Visual C++, for wasn't correctly scoped:

void foo()
{ 
  for (int i = 1; false; ) { }
  __if_exists(i) // What do you expect? VC++ let i escape.
}


来源:https://stackoverflow.com/questions/12009869/why-not-use-if-exists-with-local-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!