c++ - Offsetof Function with std::vector -


could explain me why offsetof function not work on std::vectors shown below:

#include <windows.h> #include <iostream> #include <vector>  using namespace std;  struct foostruct {     double x;     double y[10];     std::vector<double> z; }; int main() {     cout << offsetof(foostruct, x) << endl;     cout << offsetof(foostruct, y[2]) << endl;     cout << offsetof(foostruct, z[2]) << endl;      system("pause"); } 

calling offsetof(foostruct, z[2]) produces following compiling error:

cannot apply 'offsetof' when 'operator[]' overloaded

because offsetof isn't function macro, , works on pod types, or standard layout class in c++11. it's there backward compatibility c.

the reason compiler refuses allow use subscription operator, issues aside, because macro evaluated @ compile time, overloaded operator might work @ runtime calculate result.


Comments