Category : lambda

The following code snippet doesn’t compile because of the absence of copy ctor. template <typename Func> void print(Func f) { f(); } struct abc { abc() = default; abc(const abc&) = delete; abc& operator=(const abc&) = delete; void operator()() { std::cout << "f" << std::endl; } }; int main() { abc s; print(s); return 0; ..

Read more

The following code: #include <vector> extern std::vector<int> rng; int main() { auto is_even=[](int x){return x%2==0;}; int res=0; for(int x:rng){ if(is_even(x))res+=x; } return res; } is optimized by GCC 11.1 (link to Godbolt) in a very different way than: #include <vector> extern std::vector<int> rng; int main() { int res=0; for(int x:rng){ if(x%2==0)res+=x; } return res; } ..

Read more

I have been seeing code like this in submissions at leetcode.com and I don’t understand it. My unfamiliarity with the syntax has made it hard to search for an explanation. static const int _ = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }() I gather that the I/O calls are an effort to increase execution speed. ..

Read more