C++ 访问 unordered_map 的值类型

C++ accessing value type of an unordered_map

我正在查看 Adob​​e 分发的文件的代码:

https://github.com/stlab/adobe_source_libraries/blob/00ec524725ebe41b77d6d5b5d796c056cdf08585/test/json/any_json_helper.cpp

我感兴趣的部分代码是:

struct any_json_helper_t {
    typedef any                                 value_type;
    typedef string                              key_type;
    typedef string                              string_type;
    typedef unordered_map<key_type, value_type> object_type;
    typedef vector<value_type>                  array_type;
    typedef object_type::value_type             pair_type;

错误:

clang++ -o json json.cpp -std=c++14
json.cpp:105:13: error: no type named 'value_type' in 'std::__1::unordered_map<std::__1::basic_string<char>, any,
  std::__1::hash<std::__1::basic_string<char> >, std::__1::equal_to<std::__1::basic_string<char> >,
  std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, any> > >'; did you mean simply 'value_type'?
typedef object_type::value_type pair_type;
        ^~~~~~~~~~~~~~~~~
        value_t
json.cpp:100:17: note: 'value_type' declared here
    typedef any value_type;

我是不是做错了什么? (它似乎隐含地使用 any 而不是 value_type,因此找不到 object_type::value_type)。我怎样才能完成这项工作(当然除了按照编译器的建议直接使用 value_type 之外)?

编辑

any 在 c++14 中不可用(如答案中所述)。我在这种特殊情况下实现了自己的版本。

struct any
{
public:
    any() : ptr(nullptr) {}
private:
    struct base_t
    {
        virtual ~base_t() {}
    };

    base_t* ptr { nullptr };
};

class std::any 仅从 C++17 开始。 如果你想用 C++14 编译它,你可以使用来自 boost lib

boost::any calss