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

The C++ lambda version can be a foot gun: https://godbolt.org/z/Wd66GcrdG, if the return value is a struct, NRVO (named return value optimization) may be applied and the lambda will be called in different order.

As for the n3434 proposal, given that the listed implementation experiences are all macro-based, wouldn't it be more easily adopted if proposed as a standard macro like <stdarg.h>?



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...


OMG!

returns_struct looks actually correct to me, ie it is expected (by me). Golang's defer works this way.

Do both examples follow standard? Or is it common misinterpretation by all compilers?




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

Search: