samedi 7 mars 2015

Effect of subscripting the this pointer


What does this[5] do? Have I invoked some kind of undefined behavior? What about:



std::vector<decltype(this)> foo{this, this + 5u};


Is this useful? I want to know what the effects of pointer arithmetic on this are. Here's a test program:



#include <iostream>
#include <vector>

struct Foo
{
int n = 1;

void foo()
{
std::vector<decltype(this)> foo{this, this + 5u};
for (decltype(foo.size()) i = 0; i != foo.size(); ++i)
{
std::cout << foo[i]->n << "\n";
}
}
};

int main()
{
Foo{}.foo();
}

/* OUTPUT:
* 1
* 0
*/



Aucun commentaire:

Enregistrer un commentaire