tags:

views:

1740

answers:

8

PHP has a very nice function, isset($variableName). It checks if $variableName is already defined in the program or not.

Can we build similar feature for C/C++ (some kind of symbol table lookup)?

+3  A: 

Not really. You can't dynamically create variables (though you can dynamically create storage with malloc() et al, or new et al. in C++) in C. I suppose dynamically loaded libraries blur the picture, but even there, the way you establish whether the variable exists is by looking up its name. If the name is not there, then, short of running a compiler to create a dynamically loaded module and then loading it, you are probably stuck. The concept really doesn't apply to C or C++.

Jonathan Leffler
+8  A: 

I'm a C++ guy, but I remember in PHP isset is used to check if a variable contains a value when passed in through a get/post request (I'm sure there are other uses, but that's a common one I believe).

You don't really have dynamic typing in C++. So you can't suddenly use a variable name that you haven't previously explicitly defined. There really is no such thing as an "unset" variable in C++.

Even if you say "int var;" and do not initialize it, the variable has a value, usually garbage, but it's still "set" in the PHP sense.

The closes I suppose would be the preprocessor's #ifdef and #ifndef which only checks to see if you've defined a variable using #define. But in my experience this is mostly used for omitting or adding code based on flags. For example:

// code code code
#ifdef DEBUG
// debug only code that will not be included in final product.
#endif
// more code more code

You can define DEBUG using #define to determine whether to include "DEBUG" code now.

Perhaps telling a bit more about what you're trying to do with the C++ equivalent of isset will give you a better idea of how to go about doing it "The C++ Way".

Jeffrey Martinez
+3  A: 

There is no direct means of doing this in the language. However, it is possible to do this sort of thing by using a map such as the following:

typedef std::map<std::string, int> variables_type;
variables_type variables;

variables["var"] = 1;
if(variables.find("jon") == variables.end())
   std::cout << "variable, \"jon\" not set\n";

In order to make this a variable like those used in PHP or javascript, the implementation would need to use some sort of variant type.

Jon Trauntvein
+1  A: 

As said in other answers, in C++ variables are never undefined. However, variables can be uninitialised, in which case their contents are not specified in the language standard (and implemented by most compilers to be whatever happened to be stored at that memory location).

Normally a compiler offers a flag to detect possibly uninitialised variables, and will generate a warning if this is enabled.

Another usage of isset could be to deal with different code. Remember that C++ is a statically compiled language, and attempting to redefine a symbol will result in a compile time error, removing the need for isset.

Finally, what you might be looking for is a null pointer. For that, just use a simple comparison:

int * x(getFoo());
if (x) {
  cout << "Foo has a result." << endl;
} else {
  cout << "Foo returns null." << endl;
}
Paul de Vrieze
A: 

Short answer: NO

Standard followup question: What problem are you really trying to solve?

JesperE
+1  A: 

Well there is always Boost.Optional

http://www.boost.org/doc/libs/1_36_0/libs/optional/doc/html/index.html

which should almost do what you want.

A: 

You've got to separate two things here: variable declaration and variable contents.

As said in other answers, unlike PHP, C++ doesn't allow a variable to be used before it's declared.

But apart from that, it can be uninitialized.

I think the PHP isset function tries to find out if a variable has a usable value. In C++, this corresponds best to a pointer being NULL or valid.

xtofl
A: 

The closest thing I can think of is to use pointers rather than real variables. Then you can check fro NULL.

However, it does seem like you're solving wrong problem for the language, or using wrong language to solve your problem.

Arkadiy