Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Apparently I'm getting rusty. I need to find a way to have an unordered_map reference a struct using just its forward declaration, but I want STL to handle allocation and cleanup like normal. See the code for a concrete example of what I want.

Basically I am trying to make an unordered_map derived type which has itself as a value member. I am trying to use unique_ptr to do so but not having access to make_ptr I'm at a loss. I'd like to use STL's automatic heap allocators and such for this. I just want the map to be able to have itself as a value, by hook or crook. Since I'm targeting the Arduino SDK (but for 32-bit cpus) I don't think I can use boost based solutions nor use C++14.

Again see the comments in the code I've provided for a clearer understanding of the question. I'll take any alternative that approximates what I want.

Thanks in advance

What I have tried:

C++
#include <memory>
#include <string>
#include <unordered_map>
struct map_value;
struct map_value {
  // what I *really* want - works with std::map on GCC i guess
  //std::unordered_map<std::string,map_value> value;
  
  // what I'll settle for
  std::unordered_map<std::string,std::unique_ptr<map_value>> value;
};
void setup() {
  
  map_value mv;
  map_value* pmv2 = new map_value();
  // what sorcery do i use below? i don't have std::make_unique nor can I get it 
  mv.value.insert(std::make_pair("test",pmv2));
}

void loop() {
}
Posted
Updated 11-Dec-20 3:44am

Try mv.value.insert(std::make_pair("test",std::unique_ptr<map_value>(pmv2))). std::make_unique is really simply... dynamic allocation (with new) following by constructing a std::unique_ptr to hold the pointer. The constructor is explicit so it won't convert from raw pointer to unique_ptr automatically, you have to ask it.


- Ben Voigt @ stackoverflow.com

I thought I had already tried this but apparently not.
 
Share this answer
 
v2
Forward declarations work only for pointers and references. Depending which compiler you using, the "map_value" is an incomplete type at the point where you declared it inside the struct map_value body. I beleive it probably should be:

C++
struct map_value;
struct map_value {
  // what I *really* want - works with std::map on GCC i guess
  //std::unordered_map<std::string,map_value> value;
  
  // what I'll settle for
  // std::unordered_map<std::string,std::unique_ptr<map_value>> value;
  std::unordered_map<std::string,std::unique_ptr<map_value*>> value;
};
 
Share this answer
 
v2
Comments
honey the codewitch 11-Dec-20 9:45am    
unique_ptr takes the type and decorates it so you're creating a pointer to a pointer. Solution 1 is the correct answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900