Deducing this and lambda

less than 1 minute read

Published:

Today I tried to compile a C++ program but failed, which I remember to be correct.

#include <iostream>

int main() {
    auto fact = [] (this auto &&fact, int n) -> int {
        if (!(n&~1)) return 1;
        return n * fact(n-1);
    };
    std::cout << fact(5) << std::endl;
    return 0;
}

Then I looked for the C++ Standards Support in GCC and found this item:

Language FeatureAvailable in GCCSD-6 Feature Test
Deducing thisg++14__cpp_explicit_this_parameter >= 202110L

So I checked it:

>>> g++ -std=c++23 -dM -E -x c++ /dev/null | grep __cplusplus
#define __cplusplus 202302L

202302L is newer than 202110L. But it cannot compile this code.

Then I realized that g++ in MacOS is redirected to /usr/bin/g++ <-> /usr/bin/clang, which is Apple clang version 16.0.0 (clang-1600.0.26.6). i.e. MacOS uses clang as default. Deducing this requires clang-19, which I found no way to download.

Then I used homebrew to install g++-14 and set alias g++=/opt/homebrew/bin/g++-14 in ~/.zprofile.

Then this code can be compiled and executed.