1

Topic: Compile-time polymorphism == (function overloading) + (template specialization)

I understand with compile-time polymorphism. Whether correctly I understood that as a matter of fact it to be reduced to an overload of functions and specialization of templates?//function overloading void func (int) {cout <<"integer" <<endl;} void func (std:: string) {cout <<"string" <<endl;}//template specialization template <class T> struct S {const T sum (const T& a, const T& b) {return a+b;}}; template <> struct S <int> {int mult (int a, int b) {return a*b;}}; by These possibilities it is settled compile-time polymorphism? Or there is still something basic?

2

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Maxim Rogozhin, you wrote: > by These possibilities it is settled compile-time polymorphism? Or there is still something basic? Open for itself the book of Vandevurda and Dzhosattisa on templates, there is a new issuing under With ++ 11.

3

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Kernan, you wrote: K> Open for itself the book of Vandevurda and Dzhosattisa on templates, there is a new issuing under With ++ 11. Thanks, I will look! But nevertheless it would be desirable to specify a question now already...

4

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Maxim Rogozhin, you wrote: > Hello, Kernan, you wrote: K>> Open for itself the book of Vandevurda and Dzhosattisa on templates, there is a new issuing under With ++ 11. > Thanks, I will look! But nevertheless it would be desirable to specify a question now already... Read the book, there all answers to your questions.

5

Re: Compile-time polymorphism == (function overloading) + (template specialization)

> by These possibilities it is settled compile-time polymorphism? Or there is still something basic? Yes, is. CRTP. It is one of principal pillars compile time polymorphism.

6

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Kernan, you wrote: K> Hello, Maxim Rogozhin, you wrote: >> by These possibilities it is settled compile-time polymorphism? Or there is still something basic? K> open for itself the book of Vandevurda and Dzhosattisa on templates, there is a new issuing under With ++ 11. At all the answer to a question. It should be a shame with such answers.

7

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Vamp, you wrote: >> by These possibilities it is settled compile-time polymorphism? Or there is still something basic? V> yes, is. CRTP. It is one of principal pillars compile time polymorphism. I know that such CRTP, but did not carry it to something to basic, including it is valid any curious, artful reception... Here an overload of functions and  templates give clear effect - depending on type we already at a compilation stage have various functionality (and even different interfaces...). And in what adherence to principles CRTP? In that that we in base class hand over the information on type and thanks to it we can make static_cast <Derived *> (this)?

8

Re: Compile-time polymorphism == (function overloading) + (template specialization)

> I know that such CRTP, but did not carry it to something to basic, including it is valid any curious, artful reception... Well I very much the other way, consider that it and there is the main ** polymorphism ** > And in what adherence to principles CRTP? In that that we in base class hand over the information on type and thanks to it we can make static_cast <Derived *> (this)? Adherence to principles that it provides the same functionality that  polymorphism, but at a compilation stage, that is, gives the chance to define  in base class, and then to expand or redefine it in a class derivative. Practically one-to-one mapping.

9

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Vamp, you wrote: V> Adherence to principles that it provides the same functionality that  polymorphism, but at a compilation stage, that is, gives the chance to define  in base class, and then to expand or redefine it in a class derivative. Practically one-to-one mapping. It seems to me, at you  that there is a polymorphism, especially in a context of static polymorphism. It is connected to classes almost in any way without CRTP quality of static polymorphism in pluses would not decrease in any way (if to you it is very expensive CRTP I can specify "almost would not decrease in any way") an essence of polymorphism that syntactically the identical code in source codes behaves differently. In a dynamic case - behaves in  in static - differently behaves at a compilation stage. That is in final  the code different, though in source codes the code one it on fingers. In pluses dynamic polymorphism is implemented through the table of the virtual functions and consequently works only with classes (objects) of an overload of functions in pluses carry in ad hoc to polymorphism. In some languages ad hoc polymorphism is possible in  (in pluses such is not present), multimethods static polymorphism see is implemented in templates (function templates, classes and even constants) I can send only to  in more details: the link

10

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Vamp, you wrote: V> Adherence to principles that it provides the same functionality that  polymorphism, but at a compilation stage, that is, gives the chance to define  in base class, and then to expand or redefine it in a class derivative. Practically one-to-one mapping. Here it would be desirable to specify. In a case run-time polymorphism at us one general base class: class Base {virtual void func () =0;}; class A: public Base {virtual func () {};}; class B: public Base {virtual func () {}}; void doSomething (Base* obj) {obj-> func ();} doSomething (new A ()); doSomething (new B ()); And in case of usage CRTP so to make it does not turn out - two turn out simply different, in any way with each other not connected class: class A: public Base <A> {} class B: public Base <B> {} doSomething (new A ());//it does not turn out - there is no general ancestor doSomething (new B ());//it does not turn out - there is no general ancestor I.e. all the same not one-to-one mapping, so?

11

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, Maxim Rogozhin, you wrote: > Here it would be desirable to specify. In a case run-time polymorphism at us one general base class: > And in case of usage CRTP so to make it does not turn out - two turn out simply different, in any way with each other not connected class: > > class A: public Base <A> {} > class B: public Base <B> {} > doSomething (new A ());//it does not turn out - there is no general ancestor > doSomething (new B ());//it does not turn out - there is no general ancestor > At first, it turns out, but a little in other manner peculiar to static polymorphism: template <typename T> void doSomething (Base <T> * obj) {obj-> func ();} doSomething (new A ()); doSomething (new B ()); Secondly, it is necessary to understand that static polymorphism, unlike dynamic, demands knowledge of static type of objects. It leads to that typical scenarios of usage of these two types of polymorphism essentially differ and it is not necessary to search here for displays one-in-one. Static polymorphism as you validly noted, does not demand presence of the general base class. For static polymorphism operation with pointers and a dynamic storage is not characteristic. Instead of all it static polymorphism is based on presence at different types of any general properties - etc. Static polymorphism does not demand accessible operations, the bound types even that target types were classes. For example, the method std:: begin is defined both for containers, and for normal arrays. For example, we could define operation of summation of elements for the arbitrary type  for which operations begin () and end are defined () and whose member type supports addition operation: https://ideone.com/l2pNf0 #include <iostream> #include <numeric> #include <vector> template <typename T> using ElementType = std:: decay_t <decltype (*std:: begin (std:: declval <T> ()))>; template <typename T> ElementType <T> sum_of_elements ((v) {return std:: accumulate (std:: begin (v), std:: end (v), ElementType <T> ());} int main () {int array [] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std:: vector <int> vector {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std:: cout <<sum_of_elements (array) <<std:: endl; std:: cout <<sum_of_elements (vector) <<std:: endl;}

12

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, rg45, you wrote: R> At first, it turns out, but a little in other manner peculiar to static polymorphism: the bad example since it is not connected in any way with CRTP

13

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: Hello, rg45, you wrote: >> And in case of usage CRTP so to make it does not turn out - two turn out simply different, in any way with each other not connected class: >> >> class A: public Base <A> {} >> class B: public Base <B> {} >> doSomething (new A ());//it does not turn out - there is no general ancestor >> doSomething (new B ());//it does not turn out - there is no general ancestor >> R> R> template <typename T> R> void doSomething (Base <T> * obj) {obj->> func (); R>} R> R>> At first, it turns out, but a little in other manner peculiar to static polymorphism: U> the bad example since it is not connected in any way with CRTP That is as it is not connected when it as parameter accepts the pointer on objects of the classes formed by means of CRTP At desire it is possible also to strengthen accent by means of SFINAE: template <typename T> std:: enable_if_t <std:: is_base_of <Base <T>, T>:: : value> doSomething (Base <T> * obj) {obj->> func ();} Explain the thought, and that I not so understand that you mean.

14

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, rg45, you wrote: U>> the bad example since it is not connected in any way with CRTP R> That is as it is not connected when it as parameter accepts the pointer on objects of the classes formed by means of CRTP well of the full example I I do not see, probably, you in a head drew something distinct an example works with any classes and it is unimportant, whether is there CRTP. More shortly, subject CRTP + polymorphism is not uncovered look what boring example (allow to work with the full variant): https://ideone.com/Tbpfzv R> At desire it is possible also to strengthen accent by means of SFINAE: it is not necessary to reveal CRTP it is necessary to them to use and receive buns smile

15

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: U> it is not necessary to reveal CRTP it is necessary to them to use and receive buns smile I would offer such example: https://ideone.com/v6dl4U

16

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: > And in case of usage CRTP so to make it does not turn out - two turn out simply different, in any way with each other not connected class: R>> That is as it is not connected when it as parameter accepts the pointer on objects of the classes formed by means of CRTP U> well of the full example I I do not see, probably, you in a head drew something distinct U> an example works with any classes and it is unimportant, whether is there CRTP. More shortly, subject CRTP + polymorphism is not uncovered U> look what boring example (allow to work with the full variant): https://ideone.com/Tbpfzv U> it is not necessary to reveal CRTP it is necessary to them to use and receive buns smile And understood. No, I at all also did not raise at uncovering a subject "CRTP + polymorphism", it was only the counterargument to the statement "so to make it does not turn out". And generally, I am not inclined to consider not so CRTP as a basis or any integral part  polymorphism, it only the technics using static polymorphism, no more that.

17

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, rg45, you wrote: R> it only the technics using static polymorphism you can give a substantiation, what polymorphism generally at affairs here?

18

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: U> I would offer such example: https://ideone.com/v6dl4U or here so: https://ideone.com/1XGAmG

19

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: R>> it only the technics using static polymorphism U> you can give a substantiation, what polymorphism generally at affairs here? Well and why is not present - static polymorphism is used in CRTP. "Is used" is at affairs or not?

20

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, rg45, you wrote: R> Well and why is not present - static polymorphism is used in CRTP. It is not used. To give the reference on ? Well or describe more in detail that you mean

21

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: R>> Well and why is not present - static polymorphism is used in CRTP. U> it is not used. To give the reference on ? U> well or describe more in detail that mean I mean that personally it is difficult to me to present favor from CRTP which does not use static polymorphism. And on  I can to find the link itself also without effort. And I see that it is a lot of attention here is given just to static polymorphism: https://en.wikipedia.org/wiki/Curiously … e_pattern. Some use cases for this pattern are static polymorphism and other metaprogramming techniques such as those described by Andrei Alexandrescu in Modern a C ++ Design. [7] It also figures prominently in the a C ++ implementation of the Data, Context and Interaction paradigm. [8]

22

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, rg45, you wrote: R> I mean that personally it is difficult to me to present favor from CRTP which does not use static polymorphism. You would throw out from a sentence a word polymorphism and it would become clear (and the main thing - is correct) that you for binding at a compilation stage, instead of for dynamic calls (if we about usage CRTP) R> R> Some use cases for this pattern are static polymorphism and other metaprogramming techniques such as those described by Andrei Alexandrescu in Modern a C ++ Design. [7] It also figures prominently in the a C ++ implementation of the Data, Context and Interaction paradigm. [8] correct citation pulled out. I transport:" Some variants of usage represent static polymorphism "actually and on  and I hardly above gave examples with static polymorphism and at the same time with usage CRTP. The last does not attract existence of the first. You can consider me as the pedant, but then people who float in terminology, declare that CRTP - a pillar of static polymorphism, and others will simply pick up and duplicate such ideas

23

Re: Compile-time polymorphism == (function overloading) + (template specialization)

24

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, rg45, you wrote: R> terminology Questions is generally a sore point. The patient in this case is not present anything. It is enough to open  and to understand that there it is written, instead of to hang up the cliches R> Concerning static/dynamic polymorphisms, why not to use interpretation of Straustrupa:  writes all the same, as to Wikipedias (but on fingers) though in concept "uniform use of classes" the reader can enclose something the. For this reason for the outlook extension it is necessary to read some sources to understand value of term R> On the other hand, existence CRTP would appear deprived of all sense without that "uniform use of classes" for the sake of which it and has been invented incorrectly. CRTP and static polymorphism - orthogonal things. Each of them can exist in a lift-off from another. On the same  you can find examples CRTP (was specific examples "Object counter", "Polymorphic copy construction") without static polymorphism (or as you formulated "uniform use of classes")

25

Re: Compile-time polymorphism == (function overloading) + (template specialization)

Hello, uzhas, you wrote: R>> On the other hand, existence CRTP would appear deprived of all sense without that "uniform use of classes" for the sake of which it and has been invented U> incorrectly. CRTP and static polymorphism - orthogonal things. Each of them can exist in a lift-off from another. A I unless something contradicting told? Static polymorphism it is not simple can, and and exists without dependence from CRTP. CRTP in turn too can exist without static polymorphism, whether only is the useful examples of such existence much? Probably, I am mistaken, but I do not know any such case (concerning examples from Wikipedia see more low). U> on the same  you can find examples CRTP (was specific examples "Object counter", "Polymorphic copy construction") without static polymorphism (or as you formulated "uniform use of classes") Well it you already try to impose any own interpretations. From my point of view, and usage of parametrization by type of a derivative class for creation of unique counters "Object Counter", and especially, construction of object of a derivative class with explicit usage downcast in "Polymorphic Copy Construction" - all it too concerns uniform usage of derivative classes, that is to static polymorphism. Yes what to speak, formally, usage of type as template parameter is already usage!