Also solution could be flexible enough and if user return raw integer declaration could look like in Herb Sutter suggestion:
```cpp
Subscription subscribe() throws;
```
But if user return error with more complex layout than it should write it like this:
```cpp
Subscription subscribe() throws SubscriptionError;
```

On Mon, 19 Oct 2020 at 10:56, Denis Kotov <redradist@gmail.com> wrote:
The best way to return error in union
```cpp
union SubscribeResult {
    Subscription res;
    SubscriptionError err;
};
```

In such way compiler can save lots of memory and to distinguish if it is an error or return value could be added some boolean value that also returns:
```cpp
struct SubscribeResult {
    bool isError;
    union {
        Subscription res;
        SubscriptionError err;
    } result;
}
```
If there is returned error from function as integer compiler could return it as:
```cpp
struct SubscribeResult {
    Subscription res;
    int err;
}
```

Anyway compiler can choose the optimal layout for specific error that function is returns


On Mon, 19 Oct 2020 at 09:55, Florian Weimer <fweimer@redhat.com> wrote:
* Denis Kotov via Std-Discussion:

> This code:
> ```cpp
> Subscription subscribe() throws SubscriptionError;
>
> Subscription subscribe() throws {
>     ...
>     throw SubscriptionError{"Error", Payload};
>     ...
> }
> ```
> translates to:
> ```cpp
> struct SubscribeResult {
>   Subscription res;
>   SubscriptionError err;
> };
>
> SubscribeResult subscribe();
>
> SubscribeResult subscribe() {
>     ...
>     return SubscribeResult{ .err = SubscriptionError{"Error", Payload} };
>     ...
> }
> ```

Not that requires ABI work on many architectures in order to avoid
regressions when Subscription can be returned directly in a register,
but SubscribeResult cannot.  Quite a few ABIs always return structs in
memory, or pack multiple fields into a single register (optimizing for
the case where the caller stores the result to the final place, rather
than unpacking it immediately, as it would with inline exception
handling).

There is a reason why the present scheme based on unwinding has been
called “zero-cost exception handling”, too.

Thanks,
Florian
--
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill



--
Best Regards
Denis Kotov



--
Best Regards
Denis Kotov