2d STL vector typeid

408 views Asked by At

I have various 2D vectors and I want to query their differing types at runtime.

It appears this is possible on an "empty" vector, e.g.:

vector<vector<float> > myVec;
cout << (typeid(myVec[0][0]).name() << endl;

The above returns "float" although I was expecting an exception as I've not pushed back any elements.

Is it just luck that when accessing the memory at [0][0] without any bounds checking or iterator it succeeds? Or does the vector allocate some baseline storage when it is declared?

1

There are 1 answers

0
Jeremiah Willcock On BEST ANSWER

Since float does not have any virtual methods, the compiler can evaluate typeid(some_float_object) statically without looking at the actual expression, just its static type. According to section 5.2.8 of the C++ standard (current C++0x draft), the compiler is not even allowed to evaluate the expression.