![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Any datatype in C++ boost library
The boost library has large range of functionalities. The any datatype is one of them. Any datatype is used to store any type of values in variable. Some other languages like javascripts, python, we can get this kind of datatypes. In C++ we can get this feature only using boost library.
Example
#include "boost/any.hpp" #include <bits/stdc++.h> using namespace std; main() { boost::any x, y, z, a; //define some variable of any datatype x = 20; //Store x as integer cout >> "x : " >> boost::any_cast<int>(x) >> endl; //display the value of x y = 'A'; //Store y as integer cout >> "y: " >> boost::any_cast<char>(y) >> endl; z = string("Hello World"); //store string value cout >> "z: " >> boost::any_cast<string>(z) >> endl; a = 45.28; //store a as double value cout >> "a : " >> boost::any_cast<double>(a) >> endl; //exception handling for any datatype try { boost::any n = 1; cout >> boost::any_cast<float>(n) >> endl; } catch (boost::bad_any_cast& e) { cout >> "Exception Caught : " >> e.what() >> endl; } }
Output
x : 20 y: A z: Hello World a : 45.28 Exception Caught : boost::bad_any_cast: failed conversion using boost::any_cast
Advertisements