static-polymorphism

Dyamic vs Static Polymorphism in C++ : which is preferable?

我的梦境 提交于 2019-12-04 08:03:14
问题 I understand that dynamic/static polymorphism depends on the application design and requirements. However, is it advisable to ALWAYS choose static polymorphism over dynamic if possible? In particular, I can see the following 2 design choice in my application, both of which seem to be advised against: Implement Static polymorphism using CRTP: No vtable lookup overhead while still providing an interface in form of template base class. But, uses a Lot of switch and static_cast to access the

why no need of forward declaration in static dispatching via templates?

泪湿孤枕 提交于 2019-12-03 10:27:45
I am playing a bit with static polymorphism, I'm calling a function which internally calls the "right" specialized function depending on the type of the initial argument (basically I'm doing tagging). Here is the code: #include <iostream> using namespace std; // tags struct tag1{}; struct tag2{}; // the compliant types, all should typedef tag_type struct my_type1 { using tag_type = tag1; }; struct my_type2 { using tag_type = tag2; }; // static dispatch via tagging template <typename T> void f(T) { cout << "In void f<typename T>(T)" << endl; // why can I call f_helper without forward definition

Is there real static polymorphism in C++?

蓝咒 提交于 2019-12-03 02:16:22
Here is a simple code in C++: #include <iostream> #include <typeinfo> template<typename T> void function() { std::cout << typeid(T).name() << std::endl; } int main() { function<int>(); function<double>(); return 0; } I have read that templates in C++ is a compile-time feature, which is not like generics in C#/Java. So as I understood, the C++ compiler will divide a single defined function into various number (depends on calls count with different type) of functions. Am I right or not? I'm not an expert in C++ compilers, so I'm asking a piece of advice from you. If my suggestion about compiler

Dyamic vs Static Polymorphism in C++ : which is preferable?

我怕爱的太早我们不能终老 提交于 2019-12-02 19:12:23
I understand that dynamic/static polymorphism depends on the application design and requirements. However, is it advisable to ALWAYS choose static polymorphism over dynamic if possible? In particular, I can see the following 2 design choice in my application, both of which seem to be advised against: Implement Static polymorphism using CRTP: No vtable lookup overhead while still providing an interface in form of template base class. But, uses a Lot of switch and static_cast to access the correct class/method, which is hazardous Dynamic Polymorphism: Implement interfaces (pure virtual classes),

Does static polymorphism make sense for implementing an interface?

孤者浪人 提交于 2019-11-29 10:52:21
and Merry Christmas everybody! I am learning about static polymorphism and I'm reading Andrei Alexandrescu's excellent book on policy-based design. I came across the following, in my code: I have interface Interface which specifies that method Foo must be present. This interface will be implemented by class Impl . I have the following two options: 1) Dynamic polymorphism class Interface { public: virtual void Foo() = 0; } class Impl : public Interface { public: void Foo() {}; } 2) Static polymorphism class Impl { { public: void Foo() {}; } template <class I> class Interface : public I { public

Is there a generic way to adapt a function template to be a polymorphic function object?

做~自己de王妃 提交于 2019-11-28 21:34:09
I have some function templates, for example template <typename T> void foo(T); template <typename T> void bar(T); // others and I need to pass each one to an algorithm that will call it with various types, e.g. template <typename F> void some_algorithm(F f) { // call f with argument of type int // call f with argument of type SomeClass // etc. } I can't pass in my function template uninstantiated, but I can't instantiate it with any specific type either because some_algorithm will need to call it with arguments of several different types. I could adapt my function templates to be polymorphic

CRTP and multilevel inheritance

元气小坏坏 提交于 2019-11-28 15:53:08
问题 A friend of mine asked me "how to use CRTP to replace polymorphism in a multilevel inheritance" . More precisely, in a situation like this: struct A { void bar() { // do something and then call foo (possibly) in the derived class: foo(); } // possibly non pure virtual virtual void foo() const = 0; } struct B : A { void foo() const override { /* do something */ } } struct C : B { // possibly absent to not override B::foo(). void foo() const final { /* do something else */ } } My friend and I

Does static polymorphism make sense for implementing an interface?

孤人 提交于 2019-11-28 04:25:40
问题 and Merry Christmas everybody! I am learning about static polymorphism and I'm reading Andrei Alexandrescu's excellent book on policy-based design. I came across the following, in my code: I have interface Interface which specifies that method Foo must be present. This interface will be implemented by class Impl . I have the following two options: 1) Dynamic polymorphism class Interface { public: virtual void Foo() = 0; } class Impl : public Interface { public: void Foo() {}; } 2) Static

Is there a generic way to adapt a function template to be a polymorphic function object?

穿精又带淫゛_ 提交于 2019-11-27 13:57:47
问题 I have some function templates, for example template <typename T> void foo(T); template <typename T> void bar(T); // others and I need to pass each one to an algorithm that will call it with various types, e.g. template <typename F> void some_algorithm(F f) { // call f with argument of type int // call f with argument of type SomeClass // etc. } I can't pass in my function template uninstantiated, but I can't instantiate it with any specific type either because some_algorithm will need to

Static polymorphism definition and implementation [closed]

扶醉桌前 提交于 2019-11-27 07:26:30
I have some questions about the concept of static polymporhism I somethimes hear about; you may interpret them primarily in the context of C++, but I'd appreciate language-agnostic answers where applicable ( hence tagging both C++ and language-agnostic ). How do we define static polymorphism in general? As an example, I believe that the std::sort function from C++ shall be considered statically polymorphic as it depends on some interface provided by some objects which behave like iterators , and the exact behaviour under the interface of provided iterators can be determined in the compile-time