0
#include "../data-structures/RMQ.h"

struct LCA {
    int T = 0;
    vi time, path, ret;
    RMQ<int> rmq;

    LCA(vector<vi>& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {}
    void dfs(vector<vi>& C, int v, int par) {
        // ...
    }

    // ...
};

In this line rmq((dfs(C,0,-1), ret)), I don't understand how (dfs(C,0,-1), ret) forms a valid input argument (i.e. a vector<int>) for the RMQ constructor. What do the surrounding parentheses do and how are the dfs() and ret used?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
apluscs
  • 75
  • 5
  • 1
    someone is trying to be clever, `rmq((dfs(C,0,-1), ret))` is actually `dfs(C,0,-1); rmq(ret)`, without a [mre] it's difficult to say why the author decided to write "clever" rather than understandable code – Alan Birtles Jul 22 '21 at 13:14

1 Answers1

2

That's a tricky piece of code. The author is using the dreaded comma operator to first call dfs(...) and then initialize the rmq vector with the value of ret.

In short, (dfs(C,0,-1), ret) first calls dfs, discards its result, and then evaluates to ret.

Don't write code like this if you can avoid it, it's confusing.

Jonathan S.
  • 1,796
  • 5
  • 14
  • 1
    It might be worth adding that ,in this specific example, the validity of the code is reliant on the order of the members of the class, which is extremely fragile at best. –  Jul 22 '21 at 13:14