1

Here the code snippet:

#include <iostream>
#include <memory>

int func()
{
    int i = 0;
    i++;

    return i;
}

int main() {
    auto foo = [sp=std::make_shared<int>(1)](){ int num = *sp++; return num;};  //no 'operator++(int)' declared for postfix '++' [-fpermissive]

    func(); //`i++` works well
}

Since it's legal to call int i=0;i++; and *sp returns the object of int, so I think *sp++ is valid too.

John
  • 2,963
  • 11
  • 33
  • 1
    You mean `(*sp)++`? – MikeCAT Apr 10 '22 at 01:29
  • @MikeCAT think so. `*sp++` is equivalent to `(*sp)++`. Anything wrong? – John Apr 10 '22 at 01:30
  • 2
    `*sp++` is not equivalent to `(*sp)++` because postfix `++` operator has higher [precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) than unary `*` operator. – MikeCAT Apr 10 '22 at 01:31
  • I see. And `++*sp` is equivalent to `++(*sp)`, why? the prefix `++` is higher than `*` indeed. – John Apr 10 '22 at 01:37
  • If you expect the prefix `++` operator to increment its right-hand side operator, what do you expect to get incremented, if not the result of the `*` operator, because that's the only thing that exists on its right-hand side? When you have two operators on the opposite sides of an expression, their relative priorities now become important. When both operators are on the same side of an expression, there aren't that many differences of opinion, that require the involvement of operator priorities to resolve. – Sam Varshavchik Apr 10 '22 at 02:12

0 Answers0