0

I'm trying to build expression templates from maths operations in order to correctly sequence unsequenced operations (such as opertor+).

The reason is because operator co_await with operator+ appears to be unsequenced (resulting in incorrect results from generators/yielding tasks). See here C++20 Coroutines, Unexpected reordering of await_resume, return_value and yield_value.

If I can use expression templates on primitive data types I can manually sequence the execution using expression templates. This is mainly an excercise to determine whether its possible (not whether its a good idea) to detemine whether a possible work around for the issue exists.

If its not possible to get global operators overloaded for primative types then does anybody have any ideas how to inject expression templates into existing operator based maths code.


Ongoing Research


Operators require atleast 1 class type for overloads:

why C++ operator overloading requires "having at least one parameter of class type"?

So, I cannot do it for fundamental data types. So it transforms this questions focus to be around other ways to transform fundamental data type expressions into expression templates.


As per comment below from Peter. As the arguments of operator+(a,b) are unsequenced this approach will not work :(


David Ledger
  • 2,033
  • 1
  • 12
  • 27
  • I'm not sure I understand this question. Or rather, what it has to do with the linked question about `co_await` and the bugs in MSVC/GCC's implementations. – Nicol Bolas Oct 17 '20 at 02:18
  • 1
    You are aware that, conventionally, writing questions involves usage of a *question mark*? (and not a question mark that happens to be in something your refer to). In any event, you seek the impossible in C++. A prerequisite for using operator overloading to turn unsequenced operations into sequenced operations would be that the evaluation of function arguments is sequenced. That is not the case. For example, in evaluating `operator+(a,b)` (or a member form `a.operator+(b)`) the evaluations of `a` and `b` are unsequenced. (in order standards, their order of evaluation was unspecified) – Peter Oct 17 '20 at 02:40
  • Thanks Peter, I've fixed up silly title (I think, I'm not great at that). – David Ledger Oct 17 '20 at 02:50
  • My idea was to build expression templates, then execute the expression templates and store their values in lvalues, then return the value. While operator+ is commutive, the types are not, so I was hoping to work around the problem using that. I believe if I can disambiguate the order of this: `a + b + c` whether (a+b) + c or a + (b + c) Then I can do something similar for my `co_await` problem. – David Ledger Oct 17 '20 at 02:59
  • @Peter Its based around tests where the problem `co_await fn + co_await fn` goes away with this `identity(co_await fn) + identity(co_await fn)` – David Ledger Oct 17 '20 at 03:08
  • Ah darn your right! https://godbolt.org/z/rGb7EP (the last test case) The arguments are not sequenced, bummer!! Any ideas? – David Ledger Oct 17 '20 at 03:22
  • @DavidLedger: What happens if you make your coroutine machinery return a prvalue instead of a reference? – Nicol Bolas Oct 17 '20 at 03:40
  • I think I do return a prvalue, which line do you mean? – David Ledger Oct 17 '20 at 03:41
  • @DavidLedger: I don't know which line it is, because you *didn't post* the line showing how you return the value from the evaluation of the `co_await` expression. Neither here nor in your other question. Also, if you want someone to see your reply, in most cases you need to `@` them. – Nicol Bolas Oct 17 '20 at 13:26
  • @NicolBolas Ah, right tagged :) All the source is there (and in the comment above), unfortunately its already prvalue. But, values in co_await expressions are always returned from the corresponding await_resume from whatever awaitable was found (either from await_transform, operator co_await, or the object itself). – David Ledger Oct 17 '20 at 13:38
  • @NicolBolas I've added the source in the actual post for the other thread(instead of just godbolt links). This thread wasn't really about that though, it was about expression templates so I won't post it here. – David Ledger Oct 17 '20 at 13:48

0 Answers0