All Questions
Tagged with polymorphism templates
376
questions
2
votes
3
answers
154
views
In C++, compared to final or not virtual function, what is the advantage of CRTP?
I'm new to learning modern C++ programming. I have read many blogs and questions but haven't found the answer of this question. In C++, compared to final or not virtual function, what is the advantage ...
0
votes
1
answer
39
views
Why is dynamic dispatch not working within a template function?
I have the following code. There is a base class (Node) and a derived class (NumberExprNode) that each have a virtual equality method.
#include <vector>
#include <memory>
#include <...
0
votes
0
answers
70
views
How to make a Template Function in C
I'm writing a code for task which contain only two data structure(6 queues and 4 collections). But the problem is that I do not know exactly which data structures I will need until the moment of ...
1
vote
1
answer
56
views
Can you use C++ concepts in the same way as polymorphic interfaces?
I have just stumbled over C++20's "concepts", something that I've wanted in the language for years... well kind of.
Consider the code below:
#include <concepts>
#include <iostream&...
0
votes
0
answers
20
views
How to cast a unique_ptr of base class to template derived class?
I have a base class for components and a derived class for my ECS arch. I need to store different types of values so my derived class is a template class.
Base class & template derived class :
...
0
votes
1
answer
38
views
Creating an interface class with a function that takes unknown amount of arguments in C++
I want to create an interface class "TaskInterface" to use polymorphism later on.
Implement some specific class "ConcreteTaskExecutor" with known parameters at compile time.
After ...
0
votes
0
answers
100
views
Storing any data type in container inside another container
I need to have a container inside other container that stores any type of data:
std::vector<std::vector<std::any>> vectors(5);
I need to use vector methods on any element of outer vector ...
0
votes
1
answer
63
views
C++ virtual template member function [duplicate]
#include <iostream>
class Data_X {
public:
Data_X(int x): x(x) {}
int getData() {
return x;
}
private:
int x;
};
class parent {
public:
template <typename T>
T* ...
0
votes
2
answers
142
views
What are the pros and cons of template and polymorphism based approaches in C++?
I wrote a program where the type of a variable is not known at compile time. It is asked to the user at run time. In the first program version, a part of the code was repeated. The modifications ...
0
votes
1
answer
94
views
Is constraining template parameters bad practice?
In C++, it is legal to assume a template parameter has certain fields or methods according to this source. In case it does not, it will simply fail to compile.
template<class Container>
void ...
0
votes
1
answer
149
views
Is there any workaround for a virtual function template with a type constraint in this case?
If you have a concept and a class member function template like so:
template<typename T>
concept Vector2 = requires (T t) { t.x; t.y; };
struct Shape
{
bool contains(const Vector2 auto&)...
1
vote
1
answer
63
views
Why is it allowed to have a container of an abstract class as a function parameter?
Recently in our code base we have found a function that didn't compile in one specific compiler.
The scenario was something like this:
#include <vector>
#include <iostream>
class Base{
...
-1
votes
1
answer
71
views
Is Compile-time Polymorphism in C++ Templates Possible? [closed]
Consider the following code snippet:
#include <iostream>
#include <typeinfo>
template<typename T>
class TypePrinter{
public:
void print_type() {
std::cout << ...
1
vote
1
answer
147
views
Avoiding vtable pointer overhead in array of known type
This is gonna be a long one, but bear with me.
Let's say I'm writing a program that deals with animals
struct animal {
virtual ~animal() {
}
virtual std::string get_noise() const = 0;
};
...
1
vote
1
answer
80
views
storing subtypes in data structure without losing access to subtype methods
I am writing a simulation in which various agents interact with and influence each other, the code is currently structured such that all concrete classes inherit from one or more classes that define ...