tags:

views:

145

answers:

8

Hello everyone,

If the title was not clear, I will try to clarify what I am asking:

Imagine I have a variable called counter, I know I can see its current value by doing something like:

std::cout << counter << std::endl;

However, assume I have lots of variables and I don't know which I'm going to want to look at until runtime.

Does anyone know a way I can fetch the value of a variable by using its name, for example:

std::cout << valueOf("counter") << std::endl;

I feel being able to do this might make debugging large complex projects easier.

Thanks in advance for your time.


Update: All the answers provided are valid and useful, however the main point is that reflection does not exist in C++ (and after reading the link recommended it is clear why).

+1  A: 

This would need something akin to reflection or eval which C++ doesn't have.

Andreas Brinck
+1  A: 

No, C++ does not provide that facility. You may be able to hack something together with macros (or other trickery) but it's likely to be fairly ugly.

Since I can think of no good reason of the top of my head why this would be useful, perhaps you could enlighten us. When something cannot be done in a certain way, it's often good to step back to the base requirements and see if there's another way.

paxdiablo
This is extremely useful when debugging Generic Algorithms. When something fails in a generic algorithm, seeing the name of the actual type really helps. Often times, debuggers don't display this.
Thomas Matthews
+1  A: 

You can use an associative container as a std::map< std::string, your_variable_type > to link a string to a variable, assuming they are all of the same type.

If you have variable of different types, solution exists, as in boost::variant

Nikko
A: 

C++ addresses all variables via address so there is no way of just saying valueOf a variable (Languages which allow this e.g. python, perl have a runtime keeping this information)

You can implement something allowing use of a name to find a value by storing the values and their names in a std::map.

Mark
A: 

Well if you really want to do something like this, you might try using std::map (map< string, int* > ). Hovewer this would restrict you to one variable type, unless you delve into some ugly pointer magic. Either way it will be ugly and trust me you really don't want to go down this path. Why do you even need such feature? If for debugging purposes, use a debugger.

PeterK
+5  A: 

As has been mentioned, you are looking for refection in C++. It doesn't have that, and this answer explains why.

Space_C0wb0y
+1  A: 

No, not with C++ or its standard library. Of course, you can hack something up to emulate this behaviour. C++ allows you to choose methods at runtime, using polymorphism, so you can take advantage of that. In essence, you'll get the method to invoke at runtime, rather than the variable, and the method will return the vlaue:

struct Value {
    virtual ~Value();
    virtual std::string value() const = 0;
};

struct Counter : public Value {
    int v;
    std::string value() const {
        istringstream ss(v);
        return ss.str();
    }
};

struct Mounter : public Value {
    double v;
    std::string value() const {
        istringstream ss(v);
        return ss.str();
    }
};

Value* get_value();

// ...
cout << get_value()->value() << endl;

Alternatively, you can maintain a map keyed on strings, the names of the values, and then look up the values with their names.

wilhelmtell
A: 

As said already, C++ provides no reflection. But you can use some key/value mapping on your own. When only one value type (e.g. int) is required, you can use an STL associative container (map) out of the box. If you need to support multiple value types, i recommend a look into boosts Variant library.

jopa