Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

template<typename T> struct __df_st : T { [[gnu::always_inline]] inline __df_st(T g) : T(g) { // empty } [[gnu::always_inline]] inline ~__df_st() { T::operator()(); } };

#define __DEFER__(V) __df_st const V = [&](void)->void #define defer __DEFER(__COUNTER__) #define __DEFER(N) __DEFER_(N) #define __DEFER_(N) __DEFER__(__DEFER_VARIABLE_ ## N)

#include <stdio.h>

struct S { int r; ~S(){} };

(i know hn will mangle this but i won't indent this on mobile...)

people really write cpp like this or is this a intentionally obscure example?



That's from the OP blog, the intention of those macros is to generate uniquely named local lambda.

Cleaner version is like: https://github.com/llvm/llvm-project/issues/100869#issue-243...




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: