Obscure Lambda Notation

· 424 words · 2 minute read

The Code 🔗

int main()
{
	int a, b;

	auto _0 = +[]mutable{};
	decltype(_0) _[] = {_0,_0};
	a = *(int*) +_0;
	b = *(int*) +0[_];
	b = ([&]<auto >-> auto{_0(-[] ->auto{});}, b >> (sizeof(_0) - b));
	a /= !(!(int*)&[_,_0]{0[_](),[__=&_0]() -> auto{(*__)();};}) * b;
	char str[] = {a+=a-b,a-=b+1,a+a-3};

	std::cout << str << std::endl;
}

Let’s walk through this…

  1. We declare two variables a and b.
  2. We initalize a variable _0 with a pointer to an empty mutable lambda.
  3. We initialize a C-style array _ of type decltype(_0)[] with two elements, both of them being _0.
  4. We initalize a with the integer representation of _0.
  5. Wi initialize b with the integer representation of the first element of _.
    • In C++ arr[idx] and idx[arr] are interchangeable when arr is a C-style array.
  6. b is reassigned with the following expression:
    • ([&]<auto>->auto{_0(-[]->auto{});},b>>(sizeof(_0)-b))
    • If we break this down we notice there is a comma in the middle and parentheses surrounding all of this. This is a comma operator expression. Which means that only the things at the most right of the comma are returned out of the expression. That being the part:
      • b>>sizeof(_0)-b, in this case, b now has the value 6.
    • the [&]<auto>->auto{_0(-[]->auto{});} expression will:
      • create a templated lambda with an auto template parameter, missing parentheses, and captures its environment by reference and deduces its return type.
      • The body of the lambda calls the _0 lambda with an empty lambda pointer.
  7. Will divide a by the following expression:
    • !(!(int*)&[_,_0]{0[_](),[__=&_0]()->auto{(*__)();};}) * b
    • As we can see, there’s a humongous expression there on the left and that is then multiplied times b.
    • the expression on the left is a lambda that captures _ and _0 by value.
    • its body calls the first element of _
    • then it creates another lambda that captures _0 as a pointer into __
      • its body then calls this lambda by dereferencing it first.
    • Then the reference to this lambda’s place in memory is taken. While this expression is executing, this place in memory does exist.
    • This place in memory is then converted into an int*.
    • Then, this mystery number, is negated twice. Which will then convert it to a 0 if it was a null pointer or 1 if it was any other address (it has an address during the expression).
    • So all of this just evaluates to 1. Multiplying anything by 1 doesn’t matter so all of this is just obscuring that your multiplying b by 1.
  8. A char array named str is created containing our three special characters.
  9. str is sent to stdout

BOOM

:3c