1

Topic: Help with implementation

There is a library class with the interface template <class T> struct Matrix1 {}; template <class T> struct Matrix2 {}; struct Base {virtual Matrix1 <int> foo (Matrix1 <int>) = 0; virtual Matrix2 <float> foo (Matrix2 <float>) = 0; template <class T> T call_foo (T t) {return foo (t);}}; There is the second library class defining  implementation struct D1: Base {//default impl Matrix1 <int> foo (Matrix1 <int>) {std:: cout <<"default foo1\n"; return Matrix1 <int> ();} Matrix2 <float> foo (Matrix2 <float>) {std:: cout <<"default foo2\n"; return Matrix2 <float> ();} }; Also there is a user who is inherited from D1 and can redefine foo. Specificity is that that the user if redefines foo should write both variants, and inside foo the user the code for both variants the identical. It would be desirable that from the user it was required to write only one sample function, instead of 2 identical virtual, different only types. template <class D2> struct D1: Base {D1 (): der (static_cast <D2 *> (der)) {}//default impl/*Matrix1 <int> foo (Matrix1 <int>) {cout <<"default foo1\n"; return Matrix1 <int> ();} Matrix2 <float> foo (Matrix2 <float>) {cout <<"default foo2\n"; return Matrix2 <float> ();} *///if user impl foo Matrix1 <int> foo (Matrix1 <int> a) {return der-> foo (a);} Matrix2 <float> foo (Matrix2 <float> a) {return der-> foo (a);} private: D2 * der;} ;//user struct D2: D1 <D2> {template <class T> T foo (T a) {cout <<"user foo\n"; return a;}}; . if the user defined at itself sample foo, it is necessary to pull it in D1. If is not present, in D1 to use  implementation. The full code https://ideone.com/ZmsVwy

2

Re: Help with implementation

Hello, Kazmerchuk Pavel, you wrote: KP> Also there is a user who is inherited from D1 and can redefine foo. KP> Specificity is that that the user if redefines foo should write both variants, and inside foo the user the code for both variants the identical. KP> It would be desirable that from the user it was required to write only one sample function, instead of 2 identical virtual, different only types. KP> . if the user defined at itself sample foo, it is necessary to pull it in D1. If is not present, in D1 to use  implementation. Yes you, like, already started to move in a right direction, but stopped halfway, for some reason. Here a simple finishing: https://ideone.com/IwFrNC If it is important, that functions   too foo (instead of foo_impl as in my example), it it is possible to reach introduction of one more class in . [Offtop] And why all methods not constant and all parameters on value? If only to reduce a text amount it is vain - awfully cuts on eyes.

3

Re: Help with implementation

Hello, rg45, you wrote: R> Yes you, like, already started to move in a right direction, but stopped halfway, for some reason. Here a simple finishing: R> https://ideone.com/IwFrNC R> If it is important, that functions   too foo (instead of foo_impl as in my example), it it is possible to reach introduction of one more class in . Precisely! Thanks, and that I already got into sample magic... R> [Offtop] And why all methods not constant and all parameters on value? If only to reduce a text amount it is vain - awfully cuts on eyes. Yes, for brevity, I will consider)

4

Re: Help with implementation

Hello, rg45, you wrote: R> If it is important, that functions  c too foo (instead of foo_impl as in my example), it it is possible to reach introduction of one more class in hierarchy. Important! And what for one more class? Here so why it is bad? https://ideone.com/SpXxzg

5

Re: Help with implementation

Hello, Kazmerchuk Pavel, you wrote: R>> If it is important, that functions  c too foo (instead of foo_impl as in my example), it it is possible to reach introduction of one more class in hierarchy. KP> it is important! And what for one more class? Here so why it is bad? https://ideone.com/SpXxzg It seems that we speak about different things. I meant only that function which now is called foo_impl - both in mine, and in yours variants.

6

Re: Help with implementation

Hello, rg45, you wrote: R> Hello, Kazmerchuk Pavel, you wrote: R>>> If it is important, that functions  c too foo (instead of foo_impl as in my example), it it is possible to reach introduction of one more class in hierarchy. KP>> it is important! And what for one more class? Here so why it is bad? https://ideone.com/SpXxzg R> It seems that we speak about different things. I meant only that function which now is called foo_impl - both in mine, and in yours variants. Clearly, really about the different

7

Re: Help with implementation

One more question arose. Sometimes the user wants to inherit the implementation, but a part of functions to redefine. struct D4: D2 {template <class T> T foo_impl (T a) {cout <<"D4 foo_impl\n"; return a;}}; But so no redefinition foo_impl will exist. On the former it will be caused D2:: foo_impl. It is possible to make D2 sample template <class U> struct D2: D1 <U> {template <class T> T foo_impl (T a) {cout <<"D2 foo_impl\n"; return a;}}; struct D4: D2 <D4> {template <class T> T foo_impl (T a) {cout <<"D4 foo_impl\n"; return a;}}; And like it the task solves, but now how to create objects D2? D2 <???> d2; https://ideone.com/31erON

8

Re: Help with implementation

Hello, Kodt, you wrote: KP>> Specificity is that that the user if redefines foo should write both variants, and inside foo the user the code for both variants the identical. KP>> It would be desirable that from the user it was required to write only one sample function, instead of 2 identical virtual, different only types. Generally, such setting of the task means that are necessary , but in pluses  is not present (and to do them manually - it is very unprofitable). Therefore is the most easier (if not to contact CRTP) - to agree that all successors look here so As code pieces almost invariable within the limits of struggle with  it it is possible to write all on macroes. Now indeed, but macroes irritate. If it would be desirable CRTP, here so class Derived1: public BaseAdaptT <Derived1> {> public: template <class T> Matrix <T> foo_impl (Matrix <T> m) {...} };> class Derived2: public DefaultImplT <Derived2> {> public: some xyz (another qwe) {...}//implementation redefinition };> it is necessary to Redefine just foo_impl. And such foo_impl a little with a different dial-up of arguments, and it is necessary to redefine one - two.

9

Re: Help with implementation

Hello, Kazmerchuk Pavel, you wrote: KP> One more question arose. Sometimes the user wants to inherit the implementation, but a part of functions to redefine. KP> but so no redefinition foo_impl will exist. On the former it will be caused D2:: foo_impl. KP> It is possible to make D2 sample KP> And like it the task solves, but now how to create objects D2? It is possible to fasten here such crutch: https://ideone.com/y7izOE And it is possible simply  the trivial class D3 derivative from D2 <D3>. If it does not break the concept, certainly.

10

Re: Help with implementation

Hello, rg45, you wrote: R> It is possible to fasten here such crutch: https://ideone.com/y7izOE R> And it is possible simply  the trivial class D3 derivative from D2 <D3>. If it does not break the concept, certainly. Cool! Thanks, a subject it is closed.