0

I need to check if a variable "var1" is present in the current scope or not.

somefunction()
{

     ...
     ....
     {
          ......
          string var1("");
          ...
          // if i check var1..it should be in scope
          // something like inScope(var1)..return true if it is in scope else false
     }
     // if i check var1..it should be out of scope
     // something like inScope(var1)..return true if it is in scope else false
}
Sandy
  • 21
  • 2
  • 5
    press compile and look for the errors – TJD Sep 25 '12 at 18:31
  • So basically you want to test if it is valid C++ syntax or not? if you refer to the variable outside the scope your compiler should punish you. :) – Tommy Andersen Sep 25 '12 at 18:31
  • I don't know if it is possible, but I'd really like to know why you need such a thing! – Alexandre Vinçon Sep 25 '12 at 18:31
  • 2
    That is not possible. You need to rethink your design. If you explain why you want to do that, someone might be able to suggest an alternative. – Dirk Holsopple Sep 25 '12 at 18:32
  • There are compiler-specific solutions to this. I think MSVC has `__if_exists(identifier){ ... }`. – Xeo Sep 25 '12 at 18:36
  • By asking for syntax aside from simply checking if the program compiles, I get the impression that you want to venture into [tag:reflection]. What are you _really_ trying to do? – Anthony Burleigh Sep 25 '12 at 18:36
  • I want to do this without compiling that .cpp file. Basically i want to give one .cpp file as input and it this program should list out of scope variables. – Sandy Sep 25 '12 at 18:51
  • Step one: write a C++ parser. Let us know when you're finished with that for the next step. Realistically though, you'd probably be better off parsing the error messages from an existing compiler or at the very least using an existing parser. – Dirk Holsopple Sep 25 '12 at 18:58
  • @user1698072 That is a detail you might want to add to the question. – Tommy Andersen Sep 26 '12 at 06:24

1 Answers1

1

I believe you've misunderstood something at the very core of the language, or the toolchain.. Methods, classes, variables etc - they either 'exist' and 'are in scope' or not. If you try to actually use anything that "is not in the scope", this is a hard error and attempt to compile such code will usually just break. There is very little point in checking and branching the logic depending on existence of local variables.. I really think that you have overcomplicated something. If in the "later code" of your method you just want to check if something has happened earlier - why don't you create simple bool variable at the beginning of the method, initialize it to false, and set it to true only if the thing happened? and later just check the variable?

Having said that, while it is not possible to check whether a local variable is defined or not, it is completely possible to check whether a class member exists or not - due to some smart tricks with templates and SFINAE. I mean - you can test whether a class X defines a field Y or a method Z and statically get a true/false response at compilation time.

you may want to check for example: https://stackoverflow.com/a/7687190/717732 or https://stackoverflow.com/a/2133273/717732

Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107