Boost logo

Boost Users :

From: David Ohlemacher (ohlemacher_at_[hidden])
Date: 2005-07-12 12:32:38

Hi all,

According to the docs
<>, "Invoking a
property map function on an /invalid/ key results in undefined behavior."

True enough; memory is corrupted. How do I prevent this if I am not
sure my key is valid. Do I have to keep a list of valid keys separately
from my property map?


#include <iostream>
#include <map>
#include <string>
#include <boost/property_map.hpp>

template <typename AddressMap>
void foo(AddressMap address)
  typedef typename boost::property_traits<AddressMap>::value_type value_type;
  typedef typename boost::property_traits<AddressMap>::key_type key_type;

  value_type old_address, new_address;
  key_type fred = "Fred";
  old_address = get(address, fred);
  new_address = "384 Fitzpatrick Street";
  put(address, fred, new_address);

  key_type joe = "Joe";
  value_type& joes_address = address[joe];
  joes_address = "325 Cushing Avenue";

  std::map<std::string, std::string> name2address;
  boost::associative_property_map< std::map<std::string, std::string> >

                                std::string("710 West 13th Street")));
                                std::string("710 West 13th Street")));

  for (std::map<std::string, std::string>::iterator i = name2address.begin();
       i != name2address.end(); ++i)
    std::cout << i->first << ": " << i->second << "\n";

  return EXIT_SUCCESS;

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at