Skip to content

bugprone-nondeterministic-pointer-iteration-order reported with custom comparator #145665

Open
@firewave

Description

@firewave
#include <algorithm>
#include <vector>

struct S
{
    int i() const;
};

void f()
{
    std::vector<const S*> v;
    std::sort(v.begin(), v.end(), [](const S* s1, const S* s2){
        return s1->i() > s2->i();
    });
}
<source>:12:5: warning: sorting pointers is nondeterministic [bugprone-nondeterministic-pointer-iteration-order]
   12 |     std::sort(v.begin(), v.end(), [](const S* s1, const S* s2){
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   13 |         return s1->i() > s2->i();
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~
   14 |     });

https://godbolt.org/z/xjYe63rY6

This is only reported with libc++ (see #145667).

The warning should not be reported if a custom compare function is provided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions