Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using CRTP instead of standard polymorphism #13

Open
giacomo-b opened this issue Sep 29, 2021 · 3 comments
Open

Using CRTP instead of standard polymorphism #13

giacomo-b opened this issue Sep 29, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@giacomo-b
Copy link
Owner

A Curiously Recurring Template Pattern (CRTP) would probably be a better design choice instead of classic inheritance that makes use of virtual functions.

Advantage in the case of, e.g. LinearSystem and NonlinearSystem (both of which are Systems):

  1. There's need to replace System instances at runtime or to work with heterogeneous collections of Systems.
  2. CRTP would be more flexible
  3. Virtual methods imply vtable lookups at runtime. With CRTP the compiler knows which class gets instantiated, thus guaranteeing better performance.
@giacomo-b giacomo-b added the enhancement New feature or request label Sep 29, 2021
@giacomo-b
Copy link
Owner Author

C++20 concepts would be even better.

@tomlankhorst
Copy link

Best of both worlds: Better Code: Runtime Polymorphism - Sean Parent

@giacomo-b
Copy link
Owner Author

Thank you Tom, I will watch it and elaborate on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants