Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ (non MS compiler) testing if a variable exists...

Started by random_thinker Oct 16, 2007 at 3:09 PM 10 replies 13.7k views
Original Post
random_thinker
random_thinker
Is there any way to test if a variable exists in C++ generally? the MS reserved keyword for this is __if_exists(variable) or __if_not_exists(variable). But is there an Std C++ way to do this? --random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
Madhed
Madhed
Hey There.
I've never heard of the functions you mentioned.

After all it doesn't make sense to me...
Why would you need a function to tell you if a variable exists? If you declared it, it exists. If you didn't it doesn't exist. Maybe I'm just misunderstanding your question.

If you try to access a variable that doesn't exist, your compiler will spit out an error message. So no need to check at runtime.
Madhed
Madhed
Oh sorry, I just checked the msdn. __if_exists really exists ^^ didn't have to use it yet. So I cant really help you with your question.
random_thinker
random_thinker
If you check Google '__if_exists' there will be quite a lot of info on it. But it appears that it is only an MS reserved keyword.

This technique could be useful for initializing static class variables and incrementing (or carrying our other operations on) them. Something like:

template <typename T>size_t set_value(){        // I haven't used this keyword before, so take this as pseudo code...	__if_not_exists(T::value)	{		size_t T::value=0;		return T::value;	}	return ++T::value;}


Otherwise the obvious way to do this is to explicitly define each static variable or to use a global-level type of switch. But I'm open to other suggestions; maybe I'm overlooking something here.

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
Madhed
Madhed
I think you are misunderstanding the way __if_exists works. It is an compile time keyword which "inserts" code into your program if the variable/function exists in the object used in the template. So for two different template parameters passed there could be two actual functions generated by the compiler. One is for objects that have the variable the other is for objects that do'nt have the variable.

You cant use it to add a new member variable to an object since C++ is a statically typed language.

The exmaple you posted will not compile because it's simply not possible in C++.

EDIT:
Why can't you insert the static members in the class definition and initialize them like you would normally do?
random_thinker
random_thinker
I understood that __if_exists allows you to include conditional code depending upon whether the symbol exists. This is exactly what I want to do, as follows:

// The static members in this struct are only declared, not defined, therefore the // symbols don't exist.struct myStruct{    static size_t get_value(){ return value; }private:    static size_t value;        template <typename T>    friend size_t set_value();};template <typename T>size_t set_value(){        // I haven't used this keyword before, so take this as pseudo code...	__if_not_exists(T::value)	{		size_t T::value=0;		return T::value;	}	return ++T::value;}// When this is envoked, I know that the symbol for T::value does not exist.  So // set_value defines and sets the initial value to 0.  Any future calls will operate on // the now existing symbol.  The idea is to insert set_value into another struct or function// so that the private static values are set implicitly and are not accessible for other// manipulation.set_value<myStruct>();

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
mfawcett
mfawcett
Whoa, sorry. Just looked over your sample. Your sample will not work as posted, but you did say it was pseudo-code...

Madhed is right in his latest post. You can conditionally compile code, but you cannot insert variables like that.
--Michael Fawcett
random_thinker
random_thinker
Oops! So I'm up the creek without a 'proverbial' paddle...[wink]

Do you know of any other reasonable way to do this?

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
random_thinker
random_thinker
Quote:

Why can't you insert the static members in the class definition and initialize them like you would normally do?

I can do, but I don't want to expose non-const static members to alteration as public members. This means that they should be private. Now things become more complicated. The 'value' in this case is the size of a static array and if it somehow gets accidentally altered then the result will not be a happy one.

I suppose I could make two friend functions; one to define and initialize and the second to carry out operations. Then I would have to explicitly invoke the first, and could embed the second.

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.
SiCrane
SiCrane
Trying to do this kind of thing generally indicates a failure of abstraction/poor interface planning. However, if you were to do this kind of thing you could use type traits and dispatch on the trait values. A relatively extreme example shows up here. In this case AS uses reference counting to manage objects, and if an object to bound as a handle needs to have reference counting added, you would define the traits object for that type to indicate lack of reference counting. The the template code would use a special proxy object that adds reference counting.

In your case depending on the value of the traits object you can use a typedef that indicates a template type that holds that static or typedef the original object that has the static.
random_thinker
random_thinker
Si,

Thx for your help. I think that you are right about this being a design problem. I have looked into my code and have noticed that I could restructure so that the value could be computed elsewhere, and then passed to a public const static value holder, thereby eliminating the risk of alteration, without making it private. This would solve a lot of problems, and simplify the code.

But as to the original question, I can now see how nearly-impossible this could be under C or C++.

--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.