Skip to content

Inheritence-free type erasure smart pointer. Useful for storing pointers to heterogeneous types in a single container.

License

Notifications You must be signed in to change notification settings

csullivan/any_ptr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

any_ptr

A simple header-only smart pointer class that employs type erasure and annotation to safely perform run-time casts of void* to typed pointers. any_ptrs enable the storage of heterogeneous types within a single container, e.g.

    std::vector<any_ptr> ptrs; 
    ptrs.emplace_back(new std::vector<int>({4,5,6})); 
    ptrs.push_back(make_any<std::vector<double>>({7.7,8.8,9.9}));

and safe run-time casting to their progenitor types,

    for (auto& ptr : ptrs) {
        if (auto runtime_casted_ptr = std::shared_ptr<std::vector<double>>(ptr)) {
            std::cout << "Runtime cast to std::vector<double> successful!\n";
            std::cout << runtime_casted_ptr[0] << endl;
        }
    }

Adapted from Cassio Neri's implementation to be Boost-free with C++11 idioms.

About

Inheritence-free type erasure smart pointer. Useful for storing pointers to heterogeneous types in a single container.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published