简体   繁体   中英

C++ accessing value type of an unordered_map

I am looking at the code of a file distributed by Adobe:

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

The part of the code I am interested in is:

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;

Error:

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;

Am I doing something wrong? (it seems to implicitely use any instead of value_type and thus can't find object_type::value_type ). How could I make this work (beside using value_type directly of course as suggested by compiler)?

EDIT

any is not available in c++14 (as mentioned in an answer). I implemented my own version in this particular case.

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

    base_t* ptr { nullptr };
};

class std::any is only since C++17. If you want to compile it with C++14 you can use boost::any calss from boost lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM