100 questions
0 votes
2 answers
62 views
Iterating over a reference multiple times over an arbitrary iterator
The following code works: fn do_it_fun<I>(words: Vec<&str>, inputs: I) where I: AsRef<[&'static str]> { for word in words { if inputs.as_ref().into_iter()...
-1 votes
1 answer
65 views
WTF there are many definitions for polymorphism
I’m really confused about what polymorphism in OOP actually means. Everyone explains it differently, and I’m not sure which definition is correct: In some places, polymorphism is explained as dynamic ...
1 vote
2 answers
114 views
Container of std::variant of callable objects with bound parameters w/o using dynamic memory
I want to store several different callable objects (different types) in a container (e.g. std::array) in order to invoke them later. Each callable has the same return type and the same amount and type ...
1 vote
0 answers
119 views
Why is dynamic polymorphism faster than using std::variant? [duplicate]
I know that because of virtual tables, cache misses and memory allocation for pointers to the base class, dynamic polymorphism is slow. Especially when compared to static polymorphism. I decided to ...
0 votes
1 answer
75 views
Print string from crtp type being instanciated
This a snippet from the real code, but the idea is that i want to print the service type in my log. In this example i'm trying to print it, but i get an exception, and i don't know why. I have other ...
3 votes
1 answer
251 views
How does polymorphism with multiple inheritance work in assembly?
so I believe I understand polymorphism with single inheritance, where say you have classes Dog and Cat inherit from an interface Animal and Animal has a Speak and Walk function so then you generate v ...
1 vote
1 answer
159 views
Does the CRTP "pitfall workaround" negate the early binding benefits?
In order to store CRTP object pointers in a homogenous container the templated base class can itself be derived from a class common_base that defines a pure virtual interface and (if required) a ...
-4 votes
1 answer
80 views
Java choice between overloading methods from generic class
The following code could not be compiled: in generic class LM Java unable to resolve which of "static server()" methods to call. On the other hand, I'm not able to add any "instanceof&...
3 votes
2 answers
3k views
CRTP vs. virtual function as an interface or mixin
I wonder if there is any benefit of using CRTP over virtual function polymorphism if I never invoke the function from the base class (i.e., virtual dispatch)? Here are the sample code. The disassembly ...
0 votes
0 answers
26 views
How to mock an external dependency without common interface for unit test in C++ [duplicate]
I need to mock an external dependency for unit test since it is not deterministic. The dependency class cannot be abstracted out with an interface and dependency-injected into the constructor since it ...
0 votes
1 answer
156 views
Why Method hiding is also an example of compile time polymorphism? [duplicate]
My questions are: Why method hiding is also an example of compile time polymorphism while it is not overridden? Related code: class A{ public static void print(){ System.out.println("...
0 votes
2 answers
463 views
How to handle multiple inheritance of same methods or diamond problem with CRTP static polymorphism?
I want to implement polymorphism statically with CRTP. I want to create several base classes that provide functionalities. However the functionalities can be overlapping. However if they overlap, they ...
1 vote
1 answer
394 views
Compile-Time Interfaces (non-virtual)
If you want to have different public interfaces for one and the same object, you can use virtual base classes. But those have overhead (memory and space). class View1 { public: int x; } class ...
6 votes
2 answers
3k views
C++17 std::variant is slower than dynamic polymorphism?
I was following this blog and was trying to replace a dynamic polymorphism code into using std::variant and std::visit. But I can not get std::variant + std::visit to work better than a virtual struct ...
0 votes
1 answer
342 views
Is it possible to implement the state design pattern in C++ without dynamic polymorphism?
Let's say I have following C++ code class ControlAlgorithm { public: virtual void update() = 0; virtual void enable() = 0; virtual void disable() = 0; }; class Algorithm_A : public ...