Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login


All constants are variables.

devel / comp.lang.c++ / Lambda passed through a std::sort recursion

o Lambda passed through a std::sort recursionBonita Montero

Lambda passed through a std::sort recursion


  copy mid

  copy link   Newsgroups: comp.lang.c++
From: (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Lambda passed through a std::sort recursion
Date: Sun, 19 Nov 2023 12:04:18 +0100
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <ujcq3c$3pgf8$>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 19 Nov 2023 11:04:12 -0000 (UTC)
Injection-Info:; posting-host="5188d23c02781bdaeb3c144e123c43f6";
logging-data="3981800"; mail-complaints-to=""; posting-account="U2FsdGVkX18kUHe61vtK6ezWoqOhYBpvsGq5PLVfsWM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:mP/iYjrsuEmVq/C+vVT6Rae1XEA=
Content-Language: de-DE
 by: Bonita Montero - Sun, 19 Nov 2023 11:04 UTC

I was interested in whether my C++ implementations pass the lambda you
supply with std::sort through the recursion of sort by coping. libstdc++
(g++) and libc++ (clang++) copy the lambda at each recursion level. MSVC
stores the lambda only once.

#include <iostream>
#include <vector>
#include <random>
#include <unordered_map>
#include <climits>
#include <algorithm>

using namespace std;

int main( int argc, char **argv )
{ mt19937_64 mt;
uniform_int_distribution<int> uid( INT_MAX, INT_MAX );
vector<int> vi;
vi.reserve( 1'000 );
for( size_t i = vi.capacity(); i--; )
vi.emplace_back( uid( mt ) );
unordered_map<void const *, size_t> addresses;
bool invert = argc > 1;
sort( vi.begin(), vi.end(),
[&, invert = invert]( int lhs, int rhs )
if( !invert )
return lhs < rhs;
return rhs < lhs;
} );
for( auto &pCount : addresses )
cout << pCount.first << ": " << pCount.second << endl;

I think for std::sort it would be the best to store the complete
state the lambda refers to as a static or thread_local variable
that the lambda itself doesn't need any state to be copied that
there's actually no parameter passed through the recursion.

devel / comp.lang.c++ / Lambda passed through a std::sort recursion


rocksolid light 0.9.81
clearnet tor