-2

Suppose I have either:

  1. A boost::any or
  2. An std::any (and I'm using C++17)

the type of which I don't know. Is it possible for me to print, or get as a string, the name of the type that's being held by the any?

Note: Even a mangled type name - the kind you get with typeid(TR).name() - would be sufficient I can take it from there using abi::__cxa_demangle.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 2
    Did you look at the documentation? [`boost::any`](http://www.boost.org/doc/libs/1_65_0/doc/html/boost/any.html) [`std::any`](http://en.cppreference.com/w/cpp/utility/any) – Rakete1111 Aug 22 '17 at 23:04

1 Answers1

4
#include <any>
#include <iostream>
using namespace std;

namespace TestNamespace {
  class Test {
    int x{ 0 };
    int y{ 1 };
  };
}

int main()
{       
  any thing = TestNamespace::Test();

  cout << thing.type().name() << endl;
  cin.get();

  return 0;
}

Output: class TestNamespace::Test

Oh and at least in msvc, the type_info for a std template library class looks a lot uglier than say std::string (looks like: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)

Josh
  • 12,602
  • 2
  • 41
  • 47