1

Could you explain the magic behind (1+3)[a] indexing as in the following code? (taken from here)

#include <iostream>

int main(int argc, const char * argv[]) {
    int a[] = {1, 2, 3, 4, 5, 6};
    std::cout << (1 + 3)[a] - a[0] + (a + 1)[2];
}

Is this indexing some relic of C language? I thought indexing operator [] cannot take an array in it. Would the same work for e.g. std::vector?

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • *"Would the same work for e.g. std::vector"* - Do you really need to ask like this? I'd have expected you to try first, at least. – StoryTeller - Unslander Monica Oct 14 '17 at 10:33
  • Yes this is like a 'a++' but in this case is based to an index from vector in your example : (4 + 1)[a]; you must use (1) + index of the vector but you can't use (4 + 2)[a] this will give you a error warn ... sorry for my english :(. –  Oct 14 '17 at 10:48

0 Answers0