Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

It's computer hardware, of course it's worth having <g> -- Espy on #Debian


devel / comp.lang.c++ / Re: Lambda capturing "this" vector comparison function issue

SubjectAuthor
* Lambda capturing "this" vector comparison function issueAaron Gray
+* Re: Lambda capturing "this" vector comparison function issuePaavo Helde
|`* Re: Lambda capturing "this" vector comparison function issueAaron Gray
| +- Re: Lambda capturing "this" vector comparison function issueAaron Gray
| `* Re: Lambda capturing "this" vector comparison function issuePaavo Helde
|  `* Re: Lambda capturing "this" vector comparison function issueAaron Gray
|   `- Re: Lambda capturing "this" vector comparison function issueAaron Gray
`* Re: Lambda capturing "this" vector comparison function issueBonita Montero
 `- Re: Lambda capturing "this" vector comparison function issueAaron Gray

1
Lambda capturing "this" vector comparison function issue

<c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2048&group=comp.lang.c%2B%2B#2048

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:5883:0:b0:655:babe:b3f8 with SMTP id dz3-20020ad45883000000b00655babeb3f8mr11021qvb.3.1696451079997;
Wed, 04 Oct 2023 13:24:39 -0700 (PDT)
X-Received: by 2002:a05:6870:1987:b0:1dc:768d:bf6b with SMTP id
v7-20020a056870198700b001dc768dbf6bmr1299897oam.11.1696451079589; Wed, 04 Oct
2023 13:24:39 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Wed, 4 Oct 2023 13:24:39 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
Subject: Lambda capturing "this" vector comparison function issue
From: aaronng...@gmail.com (Aaron Gray)
Injection-Date: Wed, 04 Oct 2023 20:24:39 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3300
 by: Aaron Gray - Wed, 4 Oct 2023 20:24 UTC

Having an issue calling 'std::function<bool(const T*, const T*)> comp'
https://godbolt.org/z/7dTj5cKKn

#include <string>
#include <vector>
#include <functional>

class Type {
public:
Type(std::string name) : name(name) {}
std::string name;
};
class Types {
public:
Types(std::initializer_list<const Type*> types) : types(types) {}
Types(std::vector<const Type*> types) : types(types) {}
std::vector<const Type*> types;
};

typedef std::pair<const Type*, const Type*> Pair;

class TypeChecker {
public:

std::unordered_map<Pair, bool> subTypeMemorization;
bool isSubtype(const Type* lhs, const Type* rhs);
bool isSubtype(const Types* lhs, const Types* rhs);
bool isSubtype2(const Types* lhs, const Types* rhs);
};

template <class T>
std::vector<std::pair<const T, const T>> zip(std::vector< const T>* lhs, std::vector<const T>* rhs) {
assert(lhs->size() == rhs->size());
std::vector<std::pair<const T, const T>> result;
for (auto l = lhs->begin(), r = rhs->begin(); l != lhs->end(); ++l, ++r)
result.push_back(std::make_pair(*l, *r));
return result;
}

template <class T>
bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, std::function<bool(const T*, const T*)> comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (comp(*l, *r))
return false;
return true;
}

template <class T>
bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, std::function<bool(TypeChecker*, const T*, const T*)> comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (comp(*l, *r))
return false;
return true;
}

bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
return compare(lhs->types, rhs->types, [this](const Type* l, const Type* r) {
return this->isSubtype(l, r);
});
}

bool TypeChecker::isSubtype2(const Types* lhs, const Types* rhs) {
for (auto l = lhs->types.begin(), r = rhs->types.begin(); l != lhs->types.end(); ++l, ++r)
if (this->isSubtype(*l, *r))
return false;
return true;
}

Re: Lambda capturing "this" vector comparison function issue

<ufkmad$h7q6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2050&group=comp.lang.c%2B%2B#2050

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesn...@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: Lambda capturing "this" vector comparison function issue
Date: Thu, 5 Oct 2023 00:43:39 +0300
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <ufkmad$h7q6$1@dont-email.me>
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 4 Oct 2023 21:43:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="43131e28f6e7fd3389b234c340c503b9";
logging-data="565062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yTj1ZSEWrScDyq8muIaNsBbbTIHB6sj8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:3RSWPhGpKrddKrhpg3kax+QcEBE=
Content-Language: en-US
In-Reply-To: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
 by: Paavo Helde - Wed, 4 Oct 2023 21:43 UTC

04.10.2023 23:24 Aaron Gray kirjutas:
> Having an issue calling 'std::function<bool(const T*, const T*)> comp'

[...]

>
> template <class T>
> bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, std::function<bool(const T*, const T*)> comp) {
> assert(lhs.size() == rhs.size());
> for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
> if (comp(*l, *r))
> return false;
> return true;
> }

[...]

> bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
> return compare(lhs->types, rhs->types, [this](const Type* l, const Type* r) {

You do not say what issue you have, but probably one point is that with
the prev line you expect the compiler deduce type T, while no passed
argument has type T and there is a complicated conversion from a lambda
to a std::function. Apparently this confuses the compiler too much, so
it cannot deduce T.

Solution 1: specify the needed type by hand:

return compare<Type>(lhs->types, rhs->types, [this](const Type* l, const
Type* r) {

Solution 2: avoid the lambda conversion by passing it as a template
parameter:

template <class T, class F>
bool compare(const std::vector<const T*> lhs, const std::vector<const
T*> rhs, F comp) {

hth

Re: Lambda capturing "this" vector comparison function issue

<fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2053&group=comp.lang.c%2B%2B#2053

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:6214:8cc:b0:65a:f1c0:c051 with SMTP id da12-20020a05621408cc00b0065af1c0c051mr51329qvb.3.1696477079037;
Wed, 04 Oct 2023 20:37:59 -0700 (PDT)
X-Received: by 2002:a05:6808:201c:b0:3a7:7e66:2197 with SMTP id
q28-20020a056808201c00b003a77e662197mr2203642oiw.2.1696477078714; Wed, 04 Oct
2023 20:37:58 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Wed, 4 Oct 2023 20:37:58 -0700 (PDT)
In-Reply-To: <ufkmad$h7q6$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com> <ufkmad$h7q6$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
Subject: Re: Lambda capturing "this" vector comparison function issue
From: aaronng...@gmail.com (Aaron Gray)
Injection-Date: Thu, 05 Oct 2023 03:37:59 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3492
 by: Aaron Gray - Thu, 5 Oct 2023 03:37 UTC

Hi Paavo,

Okay got solution1 working, many thanks for that, I thought it would be something simple, now I am kicking myself :)

But could not see how to invoke solution 2

On Wednesday, 4 October 2023 at 22:44:00 UTC+1, Paavo Helde wrote:
> 04.10.2023 23:24 Aaron Gray kirjutas:
> > Having an issue calling 'std::function<bool(const T*, const T*)> comp'
> You do not say what issue you have, but probably one point is that with
> the prev line you expect the compiler deduce type T, while no passed
> argument has type T and there is a complicated conversion from a lambda
> to a std::function. Apparently this confuses the compiler too much, so
> it cannot deduce T.
>
> Solution 1: specify the needed type by hand:
>
> return compare<Type>(lhs->types, rhs->types, [this](const Type* l, const
> Type* r) {

template <class T>
bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, std::function<bool(const T*, const T*)> comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (comp(*l, *r))
return false;
return true;
}

bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
return compare<Type>(lhs->types, rhs->types, [this](const Type* l, const Type* r) {
return this->isSubtype(l, r);
});
}

> Solution 2: avoid the lambda conversion by passing it as a template
> parameter:
>
> template <class T, class F>
> bool compare(const std::vector<const T*> lhs, const std::vector<const
> T*> rhs, F comp) {

From your description all I could think about was doing this as it needs this passing unless I use std::bind ? :-

template <class T, class K, typename C>
bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, K* k, C comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (k->comp(*l, *r))
return false;
return true;
}

bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
return compare<Type, TypeChecker, bool (TypeChecker::*)(const Type*, const Type*)>(lhs->types, rhs->types, this, &TypeChecker::isSubtype);
}

Could you please elaborate on solution 2 ?

Many thanks,

Aaron

Re: Lambda capturing "this" vector comparison function issue

<f4cec906-4c5b-4b97-b1c1-2918fa6092fcn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2054&group=comp.lang.c%2B%2B#2054

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:2b8d:b0:76e:f294:a706 with SMTP id dz13-20020a05620a2b8d00b0076ef294a706mr1752qkb.2.1696477685961;
Wed, 04 Oct 2023 20:48:05 -0700 (PDT)
X-Received: by 2002:a05:6871:329c:b0:1d6:a9da:847 with SMTP id
mp28-20020a056871329c00b001d6a9da0847mr57862oac.0.1696477685709; Wed, 04 Oct
2023 20:48:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Wed, 4 Oct 2023 20:48:05 -0700 (PDT)
In-Reply-To: <fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
<ufkmad$h7q6$1@dont-email.me> <fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f4cec906-4c5b-4b97-b1c1-2918fa6092fcn@googlegroups.com>
Subject: Re: Lambda capturing "this" vector comparison function issue
From: aaronng...@gmail.com (Aaron Gray)
Injection-Date: Thu, 05 Oct 2023 03:48:05 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4520
 by: Aaron Gray - Thu, 5 Oct 2023 03:48 UTC

On Thursday, 5 October 2023 at 04:38:10 UTC+1, Aaron Gray wrote:
> Hi Paavo,
>
> Okay got solution1 working, many thanks for that, I thought it would be something simple, now I am kicking myself :)
>
> But could not see how to invoke solution 2
> On Wednesday, 4 October 2023 at 22:44:00 UTC+1, Paavo Helde wrote:
> > 04.10.2023 23:24 Aaron Gray kirjutas:
> > > Having an issue calling 'std::function<bool(const T*, const T*)> comp'
> > You do not say what issue you have, but probably one point is that with
> > the prev line you expect the compiler deduce type T, while no passed
> > argument has type T and there is a complicated conversion from a lambda
> > to a std::function. Apparently this confuses the compiler too much, so
> > it cannot deduce T.
> >
> > Solution 1: specify the needed type by hand:
> >
> > return compare<Type>(lhs->types, rhs->types, [this](const Type* l, const
> > Type* r) {
> template <class T>
> bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, std::function<bool(const T*, const T*)> comp) {
> assert(lhs.size() == rhs.size());
> for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
> if (comp(*l, *r))
> return false;
> return true;
> }
> bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
> return compare<Type>(lhs->types, rhs->types, [this](const Type* l, const Type* r) {
> return this->isSubtype(l, r);
> });
> }
> > Solution 2: avoid the lambda conversion by passing it as a template
> > parameter:
> >
> > template <class T, class F>
> > bool compare(const std::vector<const T*> lhs, const std::vector<const
> > T*> rhs, F comp) {
> From your description all I could think about was doing this as it needs this passing unless I use std::bind ? :-
>
> template <class T, class K, typename C>
> bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, K* k, C comp) {
> assert(lhs.size() == rhs.size());
> for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
> if (k->comp(*l, *r))
> return false;
> return true;
> }
> bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
> return compare<Type, TypeChecker, bool (TypeChecker::*)(const Type*, const Type*)>(lhs->types, rhs->types, this, &TypeChecker::isSubtype);
> }
>
> Could you please elaborate on solution 2 ?

A slightly better attempt that gets the class but needs the following too :-

template<typename T> struct ClassOf {};
template<typename Return, typename Class>
struct ClassOf<Return(Class::*)> { using type = Class; };
template< typename T> using ClassOf_t = typename ClassOf<T>::type;

template <class T, typename C, class K = ClassOf<C>::type>
bool compare(const std::vector<const T*> lhs, const std::vector<const T*> rhs, K* k, C comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (k->comp(*l, *r))
return false;
return true;
}

bool TypeChecker::isSubtype2(const Types* lhs, const Types* rhs) {
return compare<Type, bool (TypeChecker::*)(const Type*, const Type*)>(lhs->types, rhs->types, this, &TypeChecker::isSubtype);
}

Many thanks,

Aaron

Re: Lambda capturing "this" vector comparison function issue

<uflm6v$qnrq$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2055&group=comp.lang.c%2B%2B#2055

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Lambda capturing "this" vector comparison function issue
Date: Thu, 5 Oct 2023 08:48:04 +0200
Organization: A noiseless patient Spider
Lines: 3
Message-ID: <uflm6v$qnrq$1@dont-email.me>
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 5 Oct 2023 06:47:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="46799ec5c5c68a5220fc78d2a6df9da9";
logging-data="876410"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/swHgBobDeWuaxr/3KBSVNTaIOo1bmnlg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:jczpmwLy8zGz53SL/4FdzwFXWT8=
Content-Language: de-DE
In-Reply-To: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
 by: Bonita Montero - Thu, 5 Oct 2023 06:48 UTC

If you pass your vectors by value and declare them const,
i.e. you usually won't modify it, you might rather pass
the vector a a const reference.

Re: Lambda capturing "this" vector comparison function issue

<9e54adf9-85b6-4baf-bf55-fa43240806c3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2056&group=comp.lang.c%2B%2B#2056

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a37:e309:0:b0:774:20c6:7c30 with SMTP id y9-20020a37e309000000b0077420c67c30mr58559qki.12.1696505201963;
Thu, 05 Oct 2023 04:26:41 -0700 (PDT)
X-Received: by 2002:a05:6870:7688:b0:1dc:6d26:9ff with SMTP id
dx8-20020a056870768800b001dc6d2609ffmr1859555oab.6.1696505201715; Thu, 05 Oct
2023 04:26:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 5 Oct 2023 04:26:41 -0700 (PDT)
In-Reply-To: <uflm6v$qnrq$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com> <uflm6v$qnrq$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9e54adf9-85b6-4baf-bf55-fa43240806c3n@googlegroups.com>
Subject: Re: Lambda capturing "this" vector comparison function issue
From: aaronng...@gmail.com (Aaron Gray)
Injection-Date: Thu, 05 Oct 2023 11:26:41 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1453
 by: Aaron Gray - Thu, 5 Oct 2023 11:26 UTC

On Thursday, 5 October 2023 at 07:48:19 UTC+1, Bonita Montero wrote:
> If you pass your vectors by value and declare them const,
> i.e. you usually won't modify it, you might rather pass
> the vector a a const reference.

Yes they should be passed by reference, I sorry missed doing that.

Aaron

Re: Lambda capturing "this" vector comparison function issue

<ufmnbh$118hv$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2058&group=comp.lang.c%2B%2B#2058

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesn...@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: Lambda capturing "this" vector comparison function issue
Date: Thu, 5 Oct 2023 19:13:36 +0300
Organization: A noiseless patient Spider
Lines: 98
Message-ID: <ufmnbh$118hv$1@dont-email.me>
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
<ufkmad$h7q6$1@dont-email.me>
<fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 5 Oct 2023 16:13:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a819e39ba2b13cf0d0d9d88174418fc9";
logging-data="1090111"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19TsQQz6UFsQqWMzfYLwfUABr2rEFpjl7Q="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hACjXC9QBLI7W+m1iCBTxzk1d/s=
In-Reply-To: <fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
Content-Language: en-US
 by: Paavo Helde - Thu, 5 Oct 2023 16:13 UTC

05.10.2023 06:37 Aaron Gray kirjutas:
> Hi Paavo,
>
> Okay got solution1 working, many thanks for that, I thought it would be something simple, now I am kicking myself :)
>
> But could not see how to invoke solution 2
[...]
>
> Could you please elaborate on solution 2 ?

Not sure what's there to elaborate, solution 2 was meant as an example
where the original call site code would work without any change.

I.e. the following compiles fine for me with VS2022:

#include <string>
#include <vector>
#include <functional>
#include <cassert>

class Type {
public:
Type(std::string name) : name(name) {}
std::string name;
};
class Types {
public:
Types(std::initializer_list<const Type*> types) : types(types) {}
Types(std::vector<const Type*> types) : types(types) {}
std::vector<const Type*> types;
};

typedef std::pair<const Type*, const Type*> Pair;

class TypeChecker {
public:

std::unordered_map<Pair, bool> subTypeMemorization;
bool isSubtype(const Type* lhs, const Type* rhs);
bool isSubtype(const Types* lhs, const Types* rhs);
bool isSubtype2(const Types* lhs, const Types* rhs);
};

template <class T>
std::vector<std::pair<const T, const T>> zip(std::vector< const T>* lhs,
std::vector<const T>* rhs) {
assert(lhs->size() == rhs->size());
std::vector<std::pair<const T, const T>> result;
for (auto l = lhs->begin(), r = rhs->begin(); l != lhs->end(); ++l,
++r)
result.push_back(std::make_pair(*l, *r));
return result;
}

template <class T, class F>
bool compare(const std::vector<const T*> lhs, const std::vector<const
T*> rhs, F comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (comp(*l, *r))
return false;
return true;
}

template <class T>
bool compare(const std::vector<const T*> lhs, const std::vector<const
T*> rhs, std::function<bool(TypeChecker*, const T*, const T*)> comp) {
assert(lhs.size() == rhs.size());
for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
if (comp(*l, *r))
return false;
return true;
}

bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
return compare(lhs->types, rhs->types, [this](const Type* l, const
Type* r) {
return this->isSubtype(l, r);
});
}

bool TypeChecker::isSubtype2(const Types* lhs, const Types* rhs) {
for (auto l = lhs->types.begin(), r = rhs->types.begin(); l !=
lhs->types.end(); ++l, ++r)
if (this->isSubtype(*l, *r))
return false;
return true;
}

Re: Lambda capturing "this" vector comparison function issue

<195c4898-599c-4493-b2ed-4f4f3cf83a13n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2060&group=comp.lang.c%2B%2B#2060

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:1a9e:b0:774:10b8:4e7b with SMTP id bl30-20020a05620a1a9e00b0077410b84e7bmr97337qkb.1.1696533654576;
Thu, 05 Oct 2023 12:20:54 -0700 (PDT)
X-Received: by 2002:a05:6808:18c:b0:3af:c13c:b416 with SMTP id
w12-20020a056808018c00b003afc13cb416mr803577oic.6.1696533654258; Thu, 05 Oct
2023 12:20:54 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 5 Oct 2023 12:20:53 -0700 (PDT)
In-Reply-To: <ufmnbh$118hv$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
<ufkmad$h7q6$1@dont-email.me> <fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
<ufmnbh$118hv$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <195c4898-599c-4493-b2ed-4f4f3cf83a13n@googlegroups.com>
Subject: Re: Lambda capturing "this" vector comparison function issue
From: aaronng...@gmail.com (Aaron Gray)
Injection-Date: Thu, 05 Oct 2023 19:20:54 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2544
 by: Aaron Gray - Thu, 5 Oct 2023 19:20 UTC

On Thursday, 5 October 2023 at 17:13:55 UTC+1, Paavo Helde wrote:
> 05.10.2023 06:37 Aaron Gray kirjutas:
> > Hi Paavo,
> >
> > Okay got solution1 working, many thanks for that, I thought it would be something simple, now I am kicking myself :)
> >
> > But could not see how to invoke solution 2
> [...]
> >
> > Could you please elaborate on solution 2 ?
> Not sure what's there to elaborate, solution 2 was meant as an example
> where the original call site code would work without any change.
>
> I.e. the following compiles fine for me with VS2022:

> template <class T>
> bool compare(const std::vector<const T*> lhs, const std::vector<const
> T*> rhs, std::function<bool(TypeChecker*, const T*, const T*)> comp) {
> assert(lhs.size() == rhs.size());
> for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
> if (comp(*l, *r))
> return false;
> return true;
> }
>
> bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
> return compare(lhs->types, rhs->types, [this](const Type* l, const
> Type* r) {
> return this->isSubtype(l, r);
> });
> }

Oh great simple and obvious again :) My brains is obviously not thinking in C++ land again yet.

Just the TypeChecker* as the first parameter !

Many thanks,

Aaron

Re: Lambda capturing "this" vector comparison function issue

<a7397e5a-dcba-4812-8e92-682f5973fc28n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=2061&group=comp.lang.c%2B%2B#2061

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:5a50:0:b0:635:e9f6:9470 with SMTP id ej16-20020ad45a50000000b00635e9f69470mr38215qvb.5.1696542150568;
Thu, 05 Oct 2023 14:42:30 -0700 (PDT)
X-Received: by 2002:a05:6870:7f16:b0:1dd:69a:665d with SMTP id
xa22-20020a0568707f1600b001dd069a665dmr931030oab.3.1696542150350; Thu, 05 Oct
2023 14:42:30 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.cmpublishers.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 5 Oct 2023 14:42:29 -0700 (PDT)
In-Reply-To: <195c4898-599c-4493-b2ed-4f4f3cf83a13n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=81.174.241.153; posting-account=kL25OwoAAADaEwZmFnPXDuYNUKiH5X5u
NNTP-Posting-Host: 81.174.241.153
References: <c7f55de9-4eca-4e44-b21c-98a6c5427cbbn@googlegroups.com>
<ufkmad$h7q6$1@dont-email.me> <fc037b39-b865-487a-8300-69a02e1d63ecn@googlegroups.com>
<ufmnbh$118hv$1@dont-email.me> <195c4898-599c-4493-b2ed-4f4f3cf83a13n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a7397e5a-dcba-4812-8e92-682f5973fc28n@googlegroups.com>
Subject: Re: Lambda capturing "this" vector comparison function issue
From: aaronng...@gmail.com (Aaron Gray)
Injection-Date: Thu, 05 Oct 2023 21:42:30 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2781
 by: Aaron Gray - Thu, 5 Oct 2023 21:42 UTC

On Thursday, 5 October 2023 at 20:21:06 UTC+1, Aaron Gray wrote:
> On Thursday, 5 October 2023 at 17:13:55 UTC+1, Paavo Helde wrote:
> > 05.10.2023 06:37 Aaron Gray kirjutas:
> > > Hi Paavo,
> > >
> > > Okay got solution1 working, many thanks for that, I thought it would be something simple, now I am kicking myself :)
> > >
> > > But could not see how to invoke solution 2
> > [...]
> > >
> > > Could you please elaborate on solution 2 ?
> > Not sure what's there to elaborate, solution 2 was meant as an example
> > where the original call site code would work without any change.
> >
> > I.e. the following compiles fine for me with VS2022:
> > template <class T>
> > bool compare(const std::vector<const T*> lhs, const std::vector<const
> > T*> rhs, std::function<bool(TypeChecker*, const T*, const T*)> comp) {
> > assert(lhs.size() == rhs.size());
> > for (auto l = lhs.begin(), r = rhs.begin(); l != lhs.end(); ++l, ++r)
> > if (comp(*l, *r))
> > return false;
> > return true;
> > }
> >
> > bool TypeChecker::isSubtype(const Types* lhs, const Types* rhs) {
> > return compare(lhs->types, rhs->types, [this](const Type* l, const
> > Type* r) {
> > return this->isSubtype(l, r);
> > });
> > }
> Oh great simple and obvious again :) My brains is obviously not thinking in C++ land again yet.
>
> Just the TypeChecker* as the first parameter !

Sorry ignore this my mistake !

Aaron


devel / comp.lang.c++ / Re: Lambda capturing "this" vector comparison function issue

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor