C++ Logo

sg7

Advanced search

Re: [SG7] Paper: "The syntactic gap"

From: David Rector <davrec_at_[hidden]>
Date: Fri, 26 Mar 2021 15:49:42 -0400
> On Mar 26, 2021, at 3:32 PM, Barry Revzin <barry.revzin_at_[hidden]> wrote:
>
>
>
> On Fri, Mar 26, 2021 at 2:00 PM David Rector via SG7 <sg7_at_[hidden] <mailto:sg7_at_[hidden]>> wrote:
> Attached is a new paper addressing how the P2237/P2230 facilities might be expanded to be able handle the `Sum<T,U>` example discussed awhile back, and more generally subsume the capabilities of the "string-injection" approach, while avoiding its drawbacks.
>
> Hi David,
>
> I didn't understand the Sum<T, U> example before and I still don't. There's not really a description of it in the paper, just a lot of code. What problem is it demonstrating?
>
> Barry


Thanks for this feedback, I should add an example and context to the paper.

This was originally presented awhile back on this list; https://lists.isocpp.org/sg7/2021/02/0186.php is where I give a crude description of its possible utility, which Ville improves upon by suggesting this could be implemented as a generalization of the Mediator-Observer pattern (https://lists.isocpp.org/sg7/2021/02/0187.php; following that there is some good discussion about the how design patterns in general are good examples for metaprogramming, a good way to identify deficiencies.

The crude implementation in the paper might work as follows:
```
#include SumTU.h // Sum<T,U>
template <class T, class U>
class operator_plus_exists {...}; //sfinae detector

template<typename T, typename U,
         typename = std::enable_if_t<!operator_plus_exists <T, U>::value>>
Sum<T, U> operator+(T t, U u) {
  return Sum<T, U>(t, u);
}

struct A {
  int f;
  int g;
};
struct B {
  int g;
  A h;
};
struct C {
  int f;
  int g;
  B h; //NB different type from B::h
};

int main() {
  A a1{.f = 1, .g = 2};
  A a2{.f = 3, .g = 6};
  B b{ .g = 3, .h = A{.f = 4, .g = 5}}
  C c( .f = 7, .g = 1, .h = B{ .g = 3, .h = A{.f = 1, .g = 2});
  
  Sum<Sum<Sum<A,A>, B>, C> res = a1 + a2 + b + c;

  assert(res.f = 11);
  assert(res.g = 12);
   
  Sum<A,B> res2 = res.h;

  assert(res2.g == 8);
}
```





Received on 2021-03-26 14:49:45