Thread: unordered_set with pointers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    166

    unordered_set with pointers

    Hi, I'm figuring out how to work with std::unordered_set.

    Some sites indicate that you need to create custom hashing and comparison functions when using unordered_set with custom classes but is that the case if you only use pointers to custom classes?

    This is my testing code:

    Code:
    std::unordered_set<MyClass*> unord;
    unord.insert(&level.myClasses[0]);
    unord.insert(&level.myClasses[1]);
    unord.erase(&level.myClasses[0]);
    Print((int)unord.size());
    This works, the erase() removes an object and the size() afterwards is correct, but is this safe? Can I rely on that standard pointer/adress comparison will work?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Strictly speaking, pointers are only comparable if they point to different parts of the same object, such as two elements of an array or two members of the same struct.

    Two pointers from say two calls to new are not comparable in the strict sense.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    The pointers will all point to the same class type. Since pointer comparison just compares adresses, which boils down to something like an integer comparison, I'm guessing that such comparisons are "built in" to unordered_set without the need for custom hashing and comparison functions, but I want to make sure.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yeah, you kinda missed the point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Yes... Since two calls two new can never return the same memory adress you could of course never compare them and return true. But I don't get how that is relevant. A comparison would still return false, as expected.

    Are you saying that one could get false positives when comparing pointers to items in different arrays/containers?
    Last edited by DrSnuggles; 02-01-2018 at 07:26 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No, I'm saying the behaviour is undefined, so all bets are off. It might work for you today, but don't complain if some other machine gives you a segfault just for trying.

    Imagine a machine where each object exists in it's own little virtual memory space.

    Don't assume everything lives in a contiguous flat monolithic address space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    Ok in any case, all the pointers will point to objects in the same array so that should not be an issue, right? Back to the initial question... will unordered_set handle the pointer hashing/comparison without custom functions?
    Last edited by DrSnuggles; 02-01-2018 at 07:59 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should be fine then.
    < is defined for pointers within an object.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    166
    I found an answer here, pointer functionality is indeed included in unordered_set:
    The unordered associative containers std::unordered_set, std::unordered_multiset, std::unordered_map, std::unordered_multimap use specializations of the template std::hash as the default hash function.

    Each standard library header that declares the template std::hash provides enabled specializations of std::hash for std::nullptr t and all cv-unqualified arithmetic types (including any extended integer types), all enumeration types, and all pointer types.
    std::hash - cppreference.com
    And in a thread about unordered_set here:
    For pointers, which is what the question is about, there is a specialisation of std::hash in the standard library, so nothing to worry about.
    c++ - Should I use std::set or std::unordered_set for a set of pointers? - Stack Overflow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread