Maximum of three values in C++ When I need to get maximum value of A, B, and C, then I would write like:

val = A;
val = max(val, B);
val = max(val, C);


But, in this case, I need to write two "val" in the one line. And, it is cumbersome if the "val" is more complex like "val[0][1][2].element"... If it is adding value, I can write like "val += B" without writing "val" twice in the line.

I can make macro like:

#define MAXEQ(A,B) (A) = max((A), (B))


But, it looks not very intuitive.

Is there any smarter way to write these things ?
If you have access to C++11, I'd suggest using std::max with an initializer_list&lt;T&gt;. A complete minimal example:

#include &lt;algorithm&gt;
#include &lt;initializer_list&gt;
#include &lt;iostream&gt;

int main()
{
    int a = 1;
    int b = 2;
    int c = 3;

    int m = std::max({a, b, c});

    std::cout &lt;&lt; m &lt;&lt; "\n";
}