2

According to this table, function application associates to the left. What does that mean? Associativity is important when a binary operator appears multiple times, like in a - b - c. How is that relevant to function application? How would function application be different if it associated to the right instead?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 2
    [Possible duplicate?](http://stackoverflow.com/questions/12961351/does-it-make-sense-for-unary-operators-to-be-associative) – Joseph Mansfield Nov 12 '12 at 10:46
  • According to the table, it's "left-to-right", which I would say means it "associates to the left". – Steve Jessop Nov 12 '12 at 10:50
  • @sftrabbit Application is not a unary operator. It is a binary operation with lhs being a function and rhs being the argument tuple. – Pubby Nov 12 '12 at 11:00

3 Answers3

6

Something like X(y)(z)? Could be (X(y))(z) or X((y)(z))? (where X, return value of X and y are callable).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

You misunderstood that table, function application associates to the left, not the right.

It comes into play when dealing with functions which return other functions.

Here is an example where it matters:

#include <iostream>

template<typename T>
T id_1(T t) {
    t(1);
    return t;
}

typedef void (*func)(int);

void nothing(int x) {}

func print(int x) {
    std::cout << x << std::endl;
    return nothing;
}

int main() {
    std::cout << "left associative:\n";
    id_1(print)(2);
    std::cout << "right associative:\n";
    id_1((print)(2));
}

Output

left associative:
1
2
right associative:
2
Pubby
  • 51,882
  • 13
  • 139
  • 180
0

Function call is left-associative. It is another way of saying that postfix operators have higher precedence than unary operators.

ouah
  • 142,963
  • 15
  • 272
  • 331