In C++, you can double up the indirection operator:
vector<unique_ptr<string>> arr{make_unique<string>("Test")};
cout << **arr.begin() << endl;
But you can't double up the dereference operator:
cout << arr.begin()->->c_str() << endl;
Instead, you have to settle with this (IMO) less-legible alternative:
cout << (*arr.begin())->c_str() << endl;
operator->
is a unary operator that returns a pointer type, so it seems natural to be able to chain them. Is there any good reason for this limitation? Is there some parsing difficulty I'm not seeing?
Edit
In 5.2.5/3, The C++ standard specifies:
If E1 has the type “pointer to class X,” then the expression E1->E2 is converted to the equivalent form (*(E1)).E2
I just wish it was specified as:
If E1 has the type “pointer to class X,” then the expression E1-> is converted to the equivalent form (*(E1)).
It actually seems contrary for this definition to include E1 and E2, since an overloaded operator->
isn't a binary operator.