C++ inline closure analogue -
a function executes block of code twice: cnt = 0 , cnt = 1. use following implementation:
int func { int v1, v2, ... , vn; #define cnt 0 // block of code: operations v1, ... , vn #undef cnt #define cnt 1 // same block of code #undef cnt }
this code quite ugly. using inline function lead uglier piece of code: i'd need pass variables involved function reference. i'd create kind of closure.
i cannot use
struct nested { __forceinline void block(const int cnt) { // block of code }; };
because v1, ... , vn shouldn't made static performance reasons.
i tried using lambda function visual c++ 2013 fails inline inlining set "any suitable (/ob2)" (even pgo) , hurts performance.
any suggestions?
you this:
struct func_impl { int v1, v2, ..., vn; int res; template <int cnt> void blockofcode() { ... } }; int func() { func_impl f; f.blockofcode<0>(); f.blockofcode<1>(); return f; }
this should equivalent #define
solution.
edit in comments, mentioned have variables var ## cnt
. these replaced two-element array, using template argument cnt
index them:
int var[2]; var[cnt] = ...;
Comments
Post a Comment