Boost logo

Boost Users :

Subject: Re: [Boost-users] Using boost::uuids::uuid as a map index
From: Andy Tompkins (atompkins_at_[hidden])
Date: 2011-12-22 12:12:25


On Sun, Dec 11, 2011, at 12:22 AM, Klaim - Joël Lamotte wrote:

  Hi,

On Sat, Dec 10, 2011 at 17:22, Tim Burgess <[1]tim_at_[hidden]>
wrote:

All look-ups within the map based on the UUID then fail, as
adding the
object to the map is essentially a copy operation and UUID is
non-copyable,
so new unique identifiers were generated. I then tried:
Std::map <boost::uuids::uuid *, myClass*> myMap;

AFAIK uuid is copyable as it's a POD.

Correct, boost::uuids::uuid is copyable, it is a POD.

It can be used as the key in an std::map. I have created a
simple test for this and checked it into trunk
([2]http://svn.boost.org/svn/boost/trunk/libs/uuid/test/test_uuid
_in_map.cpp). It passes. I have also attached the file.

I'm not sure where the problem is with your code. Could you
provide a more complete example?

However, I don't know about this problem but I remember having
failed to make uuid a key of map (I don't remember the reason).

If that can help you, I'm using a workaround to allow this : I
convert the uuid to std::string (using the generator function)
and use it in my whole application to allow finding by uuid from
maps. That's obviously more expensive but it works fine.

Joël Lamotte

Regards,
Andy.

References

1. mailto:tim_at_[hidden]
2. http://svn.boost.org/svn/boost/trunk/libs/uuid/test/test_uuid_in_map.cpp


// (C) Copyright Andy Tompkins 2011. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.

// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// libs/uuid/test/test_uuid_in_map.cpp -------------------------------//

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <map>

#include <boost/detail/lightweight_test.hpp>

int main(int, char*[])
{
    boost::uuids::random_generator gen;

    boost::uuids::uuid u1 = gen();
    boost::uuids::uuid u2 = gen();
    boost::uuids::uuid u3 = gen();

    std::map<boost::uuids::uuid, int> uuid_map;
    uuid_map[u1] = 1;
    uuid_map[u2] = 2;
    uuid_map[u3] = 3;

    BOOST_TEST_EQ(1, uuid_map[u1]);
    BOOST_TEST_EQ(2, uuid_map[u2]);
    BOOST_TEST_EQ(3, uuid_map[u3]);

    return boost::report_errors();
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net