This is my first blog about c++17 tutorial std:any example. The class any describes a type-safe container for single values of any type. it can be considered as replacement of void*.

  • we can assign any value to any object , but to read any object we have to proper cast.

  • It gives bad any_cast if type mismatch

  • The any object has reset modifier which destroys contained object. we can also check if any object has value has_value() is an observer for any object.

  • The std::any can hold user defined data type provided it should be copy constructible.

$ g++ -Wall -Wextra -std=gnu++17



The above example is working even if we have not provided copy constructor  because in this case compiler would generate default copy constructor. Let us delete the default copy constructor and try to compile

$ g++ -Wall -Wextra -std=gnu++17


Related Contents to follow