Pergunta

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?

Foi útil?

Solução 2

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

Outras dicas

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

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top