Aesthete
Aesthete

Reputation: 18848

Subscript operator accepts variable length arguments of variable type?

Does the [] operator accept variable length arguments or variable types? Is this behavior similar to va_args or is the last parameter always passed implicitly?

Can someone explain why the following doesn't error during compilation? (tested in VS2010)

int main()
{
    typedef std::map<int, std::string> KeyValueMap;
    typedef std::vector<int> IntList;

    IntList l(10);
    int r = l[l, "C", 1];

    KeyValueMap m;
    m[m, 1, "D", 2];

    int* i = new int[10];
    int x = i["a", i, 1];

    return 0;
}

Upvotes: 0

Views: 82

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

The comma (,) is an operator which evaluates all arguments and returns the last one.

int x = i["a", i, 1]; is basically equivalent to int x = i[1];.

Upvotes: 6

Related Questions