Skip to content

Instantly share code, notes, and snippets.

@chayleaf
Created April 9, 2017 10:21
Show Gist options
  • Select an option

  • Save chayleaf/4c0458b1b7226ad166569f079b33a4d3 to your computer and use it in GitHub Desktop.

Select an option

Save chayleaf/4c0458b1b7226ad166569f079b33a4d3 to your computer and use it in GitHub Desktop.
boost::any helper
#ifndef ANY_CAST_H
#define ANY_CAST_H
#include "stdc++.h"
#include <boost/any.hpp>
using namespace std;
#define bac boost::any_cast
class cast {
private:
boost::any v;
public:
cast(const boost::any &a) {
v = a;
}
operator int() {
if (v.type == typeid(int32_t))
return bac<int32_t>(v);
else if (v.type == typeid(int64_t))
return bac<int64_t>(v);
else if (v.type == typeid(long))
return bac<long>(v);
else if (v.type == typeid(short))
return bac<short>(v);
else
return 0;
}
operator float() {
if (v.type == typeid(double))
return bac<double>(v);
else if (v.type == typeid(float))
return bac<float>(v);
else
return 0;
}
template<typename T>
operator T() {
return bac<T>(v);
}
};
#undef bac
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment