Questions tagged [static-polymorphism]

83 questions
29
votes
7 answers

CRTP and multilevel inheritance

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(); …
Cassio Neri
  • 17,293
  • 5
  • 43
  • 66
28
votes
3 answers

Static polymorphism definition and implementation

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…
Kos
  • 64,918
  • 23
  • 156
  • 223
18
votes
4 answers

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

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…
vid
  • 959
  • 1
  • 7
  • 22
15
votes
3 answers

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

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 using…
vsoftco
  • 52,188
  • 7
  • 109
  • 221
13
votes
5 answers

Is there real static polymorphism in C++?

Here is a simple code in C++: #include #include template void function() { std::cout << typeid(T).name() << std::endl; } int main() { function(); function(); return 0; } I have read that…
user2402179
12
votes
2 answers

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

I have some function templates, for example template void foo(T); template void bar(T); // others and I need to pass each one to an algorithm that will call it with various types, e.g. template void…
HighCommander4
  • 44,537
  • 22
  • 112
  • 180
10
votes
3 answers

Is emulating pure virtual function in static polymorphism using CRTP possible?

I'm trying to implement compile-time polymorphism using CRTP, and want to force the derived class to implement the function. The current implementation is like this. template struct base { void f() { …
Inbae Jeong
  • 3,823
  • 23
  • 36
9
votes
2 answers

Has CRTP no compile time check?

I was trying to implement static polymorphism using the Curiously Recurring Template Pattern, when I noticed that static_cast<>, which usually checks at compile time if a type is actually convertible to another, missed a typo in the base class…
nyarlathotep108
  • 4,663
  • 1
  • 16
  • 46
9
votes
2 answers

What is the difference between Strategy and CRTP for static polymorphism?

I want to have an interface with multiple possible implementations, selected at compile-time. I saw that CRTP is the idiom of choice for implementing this. Why is that? An alternative is the Strategy pattern, but I see no mention of this technique…
Dan Nestor
  • 2,270
  • 1
  • 20
  • 40
8
votes
3 answers

What's the use of the derived class as a template parameter?

What's the purpose of this pattern? What is it called? It looked very strange when I saw it the first time, though I have now seen it many times. template struct Base { //... }; struct Example : Base { //... };
Thomson
  • 18,073
  • 19
  • 75
  • 125
8
votes
5 answers

Static duck typing in C++

C++ has some sort of duck typing for types given by template parameters. We have no idea what type DUCK1 and DUCK2 will be, but as long as they can quack(), it will compile and run: template void let_them_quack(DUCK1*…
Michael
  • 6,634
  • 4
  • 31
  • 64
7
votes
5 answers

Depicting static polymorphism in a UML class diagram

I have an object which is instantiated during compilation according to the build configuration. As far as the surrounding software considered, the object exposes the same interface. I would like to model the fact that the instantiation decision is…
SomeWittyUsername
  • 17,203
  • 3
  • 34
  • 78
6
votes
4 answers

Scala type inference fails to note that these types are identical, whatever they are

I have a design pattern here where there is an object generator (MorselGenerator and its children), any instance of which always generates the same exact type of object (Morsels and its children), but the type checker will not let me perform any…
6
votes
2 answers

C++ interface without virtual functions

I was wondering how we can declare an interface in C++ without using virtual functions. After some internet searching I put together this solution: #include using namespace std; // Definition of a type trait to check if a class…
carlo
  • 295
  • 1
  • 4
  • 11
6
votes
2 answers

Does C++ have a static polymorphism implementation of interface that does not use vtable?

Does C++ have a proper implementation of interface that does not use vtable? for example class BaseInterface{ public: virtual void func() const = 0; } class BaseInterfaceImpl:public BaseInterface{ public: void func(){ std::cout<<"called."<
gilbertc
  • 1,017
  • 1
  • 10
  • 18
1
2 3 4 5 6