Skip to content

Use std::any for storage? #1

@T-640

Description

@T-640

Greetings!

What you have here is an excellent approach to tackle polymorphism without having to deal with pointers. This trick has let me to simplify the code of my project by a magnitude of order.

Initially I came across this technique by reading this article - https://www.fluentcpp.com/2021/01/29/inheritance-without-pointers/. It uses a slightly different approach, in particular using the std::any itself to store the class instance. Then I found your project here. Looking at both I am trying to figure out what could be the perfect such "Any of" class for me and would like to discuss a few things about it.

What do you think, which of the two approaches is more correct? On one hand using std::any simplifies the code a bit, notably taking care of copy/move constructors and assignment operators, as well as storage, so there is no need to write identical code for any of that. On the other hand, we are not really storing any object here, but rather a constrained subset of objects, so not using std::any seems to be more semantically correct, hence the "Any of" instead of the plain "Any" name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions