Counterpart of PHP's isset() in C/C++

13,373

Solution 1

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".

Solution 2

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.

Solution 3

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++.

Solution 4

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;
}
Share:
13,373
VarunGupta
Author by

VarunGupta

Updated on June 18, 2022

Comments

  • VarunGupta
    VarunGupta almost 2 years

    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)?