Alex
Template has only one reasonable problem. It should show all logic and code is open for the user. Other things it is irrelevant for my opinion.
Danya🔥
Template it is good solution but it may be also have bad performance. It depends on the architecture and on the page size.... If you have very slow memory and very fat cpu RTTI may be preferable.
This is very specific example And optimizing such stuff first - very stupid "Premature optimization is the root of all evil"
Pavel
For user of the lib or for developer...
For the user, but maybe for both I don't care about how much code will be in the library, if the interface makes sense and allows the user to write safe code
Danya🔥
And Pavel tries to optimize something that should not be optimized with careful consideration and proper benchmarks IMHO
Alex
This is very specific example And optimizing such stuff first - very stupid "Premature optimization is the root of all evil"
I did not start the performance disscutions. We need improve performance only when it needed. The readability always preferred.
Alex
But you are the admin and you are always right.
Danya🔥
Danya🔥
The thing is if you need something that have more than.. say 10 parameters.. your design is bad
Danya🔥
And the other thing you should almost never prefer void* or std::any to templates
Alex
Any function with more then 4 parameters it is a hell....
Danya🔥
10??? More then 4 make a class or configurator
Well, there were talking about hundreds and thousands.. so at this point 4 or 10 are basically the same
Pavel
And Pavel tries to optimize something that should not be optimized with careful consideration and proper benchmarks IMHO
Well, most popular ECS are highly optimized, as if that's the only point of using them. I'm not trying to go that deep, but I see that there's a room for improvements here (and yes, I have benchmarks) If I didn't use indexes that I described would actually be pretty slow.
Alex
Well, there were talking about hundreds and thousands.. so at this point 4 or 10 are basically the same
I did not payed attention. 100 parameters it is not a programmer it is a computer since professor that never created real working code out of the academy
Pavel
And the other thing you should almost never prefer void* or std::any to templates
Aren't these completely different things? std::any and void* are for dynamic polymorphism and templates for the static one. I can't implement dynamic polymorphism with only templates
Danya🔥
There are such people called C programmers, they have no static polymorphism
Or "Performance's Witnesses" that thing that they using templates hurt performance a lot
Alex
Ok in my case it is actually static, but doing it only through templates would be very painful
If you are afrade of an error of templates chatGPT helps very well with it.
Danya🔥
You mean dynamic?
Can you name example of static polymorphism in C?
Alex
Char a = 10
Danya🔥
So?
Alex
Implicit conversion is a static polymorphism in my opinion.
Danya🔥
No, for sure it is not
Danya🔥
I'm gonna explain in a moment
Pavel
If you are afrade of an error of templates chatGPT helps very well with it.
Not errors, horrors of parametrizing everything with every type that is going to be used. Imagine writing a vector each instance of which you need to parametrize with every type you want to store in vectors thoughout your application.
Alex
Polymorphism is a option to use a different object types as one.
Danya🔥
Polymorphism is "one name for different things" What you showed as an example "different names for one thing"
Danya🔥
So?
Pavel
You have a typedef
I think we're talking about different things to be honest
Alex
I think we're talking about different things to be honest
Any complicated types with a lot of template parameters you may declare as typedef.
Alex
And it very simplify the code readability.
Danya🔥
You actually need it
Danya🔥
Like IPlayer, IGameObject - whatever
Danya🔥
And then specify only those interfaces as template parameters for example
Danya🔥
I guess you have a lot of similar classes
Alex
So?
I was wrong. You are right. I mess static polymorphism with static cast. Two years of python programming makes me stupid. No, the C programming language does not have a built-in mechanism for static polymorphism like C++ does. Static polymorphism, also known as compile-time polymorphism, is achieved in C++ through features such as function overloading, templates, and inheritance.
Danya🔥
Pavel
Like IPlayer, IGameObject - whatever
Oh well I was using that for a few years, but it was getting pretty messy very fast, so I decided to switch to ECS instead. Basically what people usually go to from that approach is either component system, similar to what Unity has, or to ECS. I decided to go with ECS, as it sounded more fun
Azadi
And how is this a polymorphism feature?
It’s a compile-time thing in itself. Let me show you a code.
Alex
In C++, a constexpr expression is an expression that can be evaluated at compile-time. The keyword "constexpr" is used to declare a function, object, or variable as being eligible for evaluation at compile-time
Danya🔥
It’s a compile-time thing in itself. Let me show you a code.
You didn't answer my question and your example probably will not
Azadi
You didn't answer my question and your example probably will not
See and you will find out: struct Animal { virtual constexpr ~Animal() = default; virtual constexpr std::size_t num_legs() const = 0; }; struct Duck : Animal { constexpr std::size_t num_legs() const override { return 2; } }; struct Cat : Animal { constexpr std::size_t num_legs() const override { return 4; } }; constexpr std::size_t count_legs(const auto animals) { std::size_t legs{ 0 }; for (const auto a : animals) legs += a->num_legs(); return legs; } int main() { constexpr Duck d; constexpr Cat c; static_assert(count_legs(std::array<const Animal*, 2>{&d, & c}) == 6); } C++20 new feature: Compile time polymorphism!
Danya🔥
In C++, a constexpr expression is an expression that can be evaluated at compile-time. The keyword "constexpr" is used to declare a function, object, or variable as being eligible for evaluation at compile-time
That's not quite right If a function is constexpr, it can be evaluated both in compile-time or runtime If a variable is constexpr, it's always evaluated at compile-time
Danya🔥
I feel sorry for the compiler developers
Danya🔥
You're right But it's stupid.. Yet constexpr itself is not polymorphic It's just runtime polymorphism is now also available at compile-time
Danya🔥
Alex
In 20 it doe.
Alex
It's important to note that constexpr expressions have certain restrictions. They must be composed of a subset of the C++ language that can be evaluated at compile-time, and they must be free of side effects. However, C++11, C++14, and C++17 introduced enhancements to the constexpr mechanism, expanding the range of expressions and functions that can be evaluated at compile-time.
Danya🔥
What I stated is true for all standards
Alex
So I need a sleep. You are very smart. Good night.
Azadi
Reread my message again and again
Define variable! An ‘std::vector’ is a variable object which cannot be constexpr. But I got what you meant. E.g., constexpr static size_t n {5};
Danya🔥
Constexpr function can be called at runtime In fact, they are always called at runtime unless they are in a constant expression. There are very few places where the constant expression exists. Like the initializer of a constexpr variable
Azadi
That’s fine anyway
Danya🔥
Here.
Please tell me exact words, I actually don't understand
Azadi
When you want complete guarantees on an object to be evaluated at compile time, as Turner said, you must declare it as static constexpr If guess, consteval will do the same too.
Azadi
Too late here.
Danya🔥
When you want complete guarantees on an object to be evaluated at compile time, as Turner said, you must declare it as static constexpr If guess, consteval will do the same too.
No, a constexpr variable must be initialized with a constant expression So, it's always initialized at compile-time and can be used in other constant expressions So, it's always evaluated at compile-time
Danya🔥
You can call a constexpr function with non-constexpr-variables But you cannot initialize a constexpr variable with non-constexpr expression
Danya🔥
On the other hand, if you don't use a constexpr variable in the other constant expression, it may be delayed to the runtime to evaluate it. But the thing is that in reality it never will Because the most classic optimizations like constant elimination and constant propagation will remove this code
Danya🔥
Turner is a great dude but sometimes he's a little bit too dramatic
Anonymous
Guys can anyone tell if anyone's have to participate in gsoc he has to know DSA in programming language and a development skill Right??
Israel
#include <iostream> using namespace std; Int main() { cout<<"Hello World"; return 0; }