0

In C++ is there any function that returns "true" when the variable is defined or false in vice versa. Something like this:

bool isDefined(string varName)
{
    if (a variable called "varName" is defined)
      return true;
    else
      return false;
}
Narek
  • 38,779
  • 79
  • 233
  • 389
  • 4
    Do you have a particular problem that you feel such a construct solves? – Richard Corden Sep 24 '09 at 12:01
  • Please tell us why you need to test for this. By default all objects are defined because of their constructor. POD types may be potentially have random values but it is considered bad programming style to declare them without a definition. – Martin York Sep 24 '09 at 16:13

8 Answers8

11

C++ is not a dynamic language. Which means, that the answer is no. You know this at compile time, not runtime.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 3
    static/dynamic is orthogonal to this issue. A static language can certainly support a runtime system in which it keeps track of known variables. Java, a static language, can query whether or not variables are defined via reflection as an example. – Falaina Sep 24 '09 at 11:27
  • Falaina, I wouldn't say it is orthogonal, but what you say makes sense. I'd be happy if you give me a better term, but I'm not unhappy enough to do my own research. – Michael Krelin - hacker Sep 24 '09 at 12:10
  • Oh, true. Orthogonal is a bit strong as it's certainly more common a feature in dynamic languages. I believe the closest term would be "supports reflection", as I *think* this behavior in general is referred to as reflection. – Falaina Sep 24 '09 at 13:10
  • 1
    Would one say that c++ is not a reflective language? ;-) Actually, reflection (*I think*) is a technique allowing program to adjust its behavior based on its structure. In a sense, template metaprogramming makes c++ reflect, it's just that it happens at compile time, still it's program code that affects itself. I'd say that our discussion may go off the tangent and beyond the realm of OP's understanding (no offense intended, of course - it's normal). – Michael Krelin - hacker Sep 24 '09 at 13:34
  • I'd say if your language allows this (or for that matter, reflection in general), then it is not entirely static. A static language won't allow this. Java, because it supports reflection, is a hybrid, with some dynamic elements. – jalf Sep 24 '09 at 15:33
  • jalf, what's a static language, anyway? :) – Michael Krelin - hacker Sep 24 '09 at 15:46
6

There is no such a thing in runtime as it doesn't make sense in a non-dynamic language as C++.

However you can use it inside a sizeof to test if it exists on compile time without side-effects.

(void)sizeof(variable);

That will stop compilation if var doesn't exist.

Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105
  • You meant at runtime, I believe. – Michael Krelin - hacker Sep 24 '09 at 11:18
  • 3
    Actually, unless I'm mistaken, it IS at compile time. sizeof is an operator, and just like you can't do: x++ without defining x, you can't do sizeof(x) without defining x. – Edan Maor Sep 24 '09 at 11:32
  • What's so special about sizeof? It will not compile even if you try to do any other operation. – Naveen Sep 24 '09 at 11:38
  • That is the quid. If the variable is not defined you'll get a compiler-error, if it is defined no side-effects because sizeof is not compiled in. Thats what I call a static-check. Compile time check. – Arkaitz Jimenez Sep 24 '09 at 11:40
1

As already stated, the C++ runtime system does not support the querying of whether or not a variable is declared or not. In general a C++ binary doesn't contain information on variable symbols or their mappings to their location. Technically, this information would be available in a binary compiled with debugging information, and you could certainly query the debugging information to see if a variable name is present at a given location in code, but it would be a dirty hack at best (If you're curious to see what it might look at, I posted a terrible snippet @ Call a function named in a string variable in C which calls a C function by a string using the DWARF debugging information. Doing something like this is not advised)

Community
  • 1
  • 1
Falaina
  • 6,625
  • 29
  • 31
1

Microsoft has two extensions to C++ named: __if_exists and __if_not_exists. They can be useful in some cases, but they don't take string arguments.

If you really need such a functionality you can add all your variables to a set and then query that set for variable existance.

Cristian Adam
  • 4,749
  • 22
  • 19
0

No. It's not like you have a runtime system around C++ which keeps remembers variables with names in some sort of table (meta data) and lets you access a variable through a dynamically generated string. If you want this, you have to build it yourself, for example using a std::map that maps strings to some objects.

Some compile-time mechanism would fit into the language. But I don't think that it would be any useful.

sellibitze
  • 27,611
  • 3
  • 75
  • 95
0

Already mentioned that C++ doesn't provide such facility.

On the other hand there are cases where the OS implement mechanisms close to isDefined(),
like the GetProcAddress Function, on Windows.

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
0

In order to achieve this first you need to implement a dynamic variable handling system, or at least find some on the internet. As previously mentioned the C++ is designed to be a native language so there are no built-in facilities to do this. What I can suggest for the most easy solution to create a std::map with string keys storing global variables of interest with a boost::any, wxVariant or something similar, and store your variables in this map. You can make your life a bit easier with a little preprocessor directive to define a variables by their name, so you don't need to retype the name of the variable twice. Also, to make life easier I suggest to create a little inline function which access this variable map, and checks if the given string key is contained by the map. There are implementation such a functionality in many places, the runtime property handling systems are available in different fashion, but if you need just this functionality I suggest to implement by yourself, because most of these solutions are quite general what you probably don't need.

progician
  • 176
  • 3
0

You can make such function, but it wouldn't operate strings. You would have to send variable name. Such a function would try to add 0 to the variable. If it doesn't exists, an error would occur, so you might want to try to make exception handling with try...throw...catch . But because I'm on the phone, I don't know if this wouldn't throw an error anyways when trying to send non-existing variable to the function...