100 questions
0
votes
2
answers
58
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
vote
2
answers
111
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 ...
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 ...
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 ...
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
1
answer
155
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("...
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 ...
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 ...
1
vote
1
answer
385
views
Can I use template member functions as an alternative to the CRTP in order to implement static polymorphism?
I want to implement the template method pattern using static polymorphism in C++.
Right now I have two classes A and B which are very similar. They have an identical public API and the exact same list ...
0
votes
1
answer
687
views
Static polymorphism using std::variant
I'm trying to implement something like static polimorphism using std::variant. I want to declare methods using VARIANT_METHOD or VARIANT_METHOD_CONST, that should take return type, method name, ...
1
vote
0
answers
152
views
Different ways of event/task processing
An event/task instance is created based on some incoming data in a thread and is sent to be processed in another thread. I'm trying to measure different implementations of processing those events in ...
1
vote
0
answers
60
views
What's the preferred way of providing customization points for an API that contains templates?
Let's say I'm writing a library and at some point, there is code that wants to do something specific with objects of user-provided types. Take swap as an example, although the function could be ...
1
vote
2
answers
3k
views
Templated member variables in C++
Often when writing templated code, I find myself needing to store an instance of the template type in a member variable. For example, I might need to cache a value to be used later on. I would like to ...
0
votes
0
answers
128
views
"No member named" incomplete type in multi-level CRTP
I have encountered an incomplete type error in designing my library's CRTP inheritance structure. I am cognizant of the fact that this might possibly be solved by way of type traits but I do not even ...
3
votes
2
answers
158
views
Static polymorphism with forwarding references
I am trying to use static polymorphism like in the simple example shown below.
#include <iostream>
template <typename Derived>
struct Base
{
decltype(auto) foo() { return static_cast&...
2
votes
2
answers
73
views
How can I use static polymorphism to convert between int and pointer types?
I have this piece of code:
#include <iostream>
template<class T> void* ToPtr(T t) { return ToPtr((void*)t); }
void* ToPtr(void* i) { return i; }
void* ToPtr(int i) { return (void*)(long)(...
3
votes
2
answers
631
views
static polymorphism and template Containers
I'm trying to use the static polymorphism and template to create a container that can hold more the one type, from what I know about template it can't be done, but I'm hoping that I'm wrong and there ...
0
votes
1
answer
226
views
Static / Compile-time Polymorphism [duplicate]
Why method overloading called as static or compile-time polymorphism
sample in Java.
class StaticPolymorphismSample {
void polymorphicMethod(int a) {
}
void polymorphicMethod(int a, int ...
0
votes
1
answer
483
views
CRTP - Is it possible to make an abstract base class?
I'm implementing a static polymorphism:
template<typename T>
class Base {
public:
void method() {
// do something
impl();
// do something else
}
void impl(...
0
votes
1
answer
898
views
"Static polymorphism with Qt signal/slot: What is wrong?"
I'm trying to use static polymorphism instead of dynamics polymorphism with Qt signal/slot mechanism. But I get compile error. What is wrong in my code? What is workaround?
devices.h
#ifndef ...
4
votes
4
answers
714
views
How can I avoid code duplication of a static method in multiple classes
I have multiple classes containing duplicated code, especially members and most important a static method that will create a new instance of the class and returning this instance: either a previously ...
0
votes
0
answers
278
views
Polymorphic call of this.constructor
WRONG QUESTION. EXAMPLE CODE WORKS.
Consider this code
export interface IParameterType{
name:string,
label:string,
type:string,
defaultValue:number
}
class Object3D{
public static ...
-1
votes
1
answer
254
views
Alternatives to abstract base class for storage
I'm wondering about the available alternatives to run-time polymorphism, specifically, an alternative to having a common base class between my classes in order to store and interact with instances of ...
0
votes
1
answer
83
views
C++ Polymorphic Static Changeable Child Function Pointer
I have a class A that is a child of class B, I need all children of class B to have a function pointer to a function for handling the class that is changeable by an external function in a different ...
0
votes
1
answer
407
views
CRTP static polymorphism: is it possible to replace the base class with a mock?
I use CRTP static polymorphism in a websocket server to decouple networking code from business logic. The base class calls methods on the derived to process messages, the derived in turn calls the ...