Date: Sun, 21 Feb 2021 09:58:23 +0100
On 19.02.21 15:34, David Rector wrote:
> Here is another class of non-trivial examples to consider:
>
> ```
> template<class T, class U>
> class Sum {
> T t;
> U u;
> public:
> Sum(T &t, U u) : t(t), u(u) {}
>
>
> // Methods: the union of the methods of T and U.
> // Wherever they "share" a method, such that names and
> // signatures of method reflections m_t and m_u are the same
> // (not necc. via inheritance) that method is implemented
> // to return t.[:m:](…) + u.[:m:](…). Otherwise, it returns
> // t.[:m:](…) or u.[:m:](…) individually.
>
> // Conversion operators: construct from the relevant fields,
> // but for any data T shares with U, need to add in the other’s
> // data to the initializer.
> explicit operator T();
> explicit operator U();
> };
I understand the composition with forwarding functions. But what would
be a real-world use case for `t.[:m:](…) + u.[:m:](…)`?
Note that `operator+` might not be defined for the return type. Also,
the signatures might differ in just the return type.
> Here is another class of non-trivial examples to consider:
>
> ```
> template<class T, class U>
> class Sum {
> T t;
> U u;
> public:
> Sum(T &t, U u) : t(t), u(u) {}
>
>
> // Methods: the union of the methods of T and U.
> // Wherever they "share" a method, such that names and
> // signatures of method reflections m_t and m_u are the same
> // (not necc. via inheritance) that method is implemented
> // to return t.[:m:](…) + u.[:m:](…). Otherwise, it returns
> // t.[:m:](…) or u.[:m:](…) individually.
>
> // Conversion operators: construct from the relevant fields,
> // but for any data T shares with U, need to add in the other’s
> // data to the initializer.
> explicit operator T();
> explicit operator U();
> };
I understand the composition with forwarding functions. But what would
be a real-world use case for `t.[:m:](…) + u.[:m:](…)`?
Note that `operator+` might not be defined for the return type. Also,
the signatures might differ in just the return type.
Received on 2021-02-21 02:58:27