Difference between revisions of "cpp/filesystem/path/operator cmp"
(templatize description for hidden friends) |
Andreas Krug (Talk | contribs) m (fmt, {{c}}) |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{title|1=operator==,!=,<,<=,>,>={{small|(std::filesystem::path)}}}} | + | {{title|1=operator==,!=,<,<=,>,>={{small|(std::filesystem::path)}}}} |
{{cpp/filesystem/path/navbar}} | {{cpp/filesystem/path/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl | num=1 | since=c++17 | 1= | + | {{dcl|num=1|since=c++17|1= |
− | bool operator==( const path& lhs, const path& rhs ) noexcept; | + | bool operator==( const path& lhs, const path& rhs ) noexcept; |
}} | }} | ||
− | {{dcl | num=2 | since=c++17 | 1= | + | {{dcl|num=2|since=c++17|1= |
− | bool operator!=( const path& lhs, const path& rhs ) noexcept; | + | bool operator!=( const path& lhs, const path& rhs ) noexcept; |
}} | }} | ||
− | {{dcl | num=3 | since=c++17 | 1= | + | {{dcl|num=3|since=c++17|1= |
− | bool operator<( const path& lhs, const path& rhs ) noexcept; | + | bool operator<( const path& lhs, const path& rhs ) noexcept; |
}} | }} | ||
− | {{dcl | num=4 | since=c++17 | 1= | + | {{dcl|num=4|since=c++17|1= |
− | bool operator<=( const path& lhs, const path& rhs ) noexcept; | + | bool operator<=( const path& lhs, const path& rhs ) noexcept; |
}} | }} | ||
− | {{dcl | num=5 | since=c++17 | 1= | + | {{dcl|num=5|since=c++17|1= |
− | bool operator>( const path& lhs, const path& rhs ) noexcept; | + | bool operator>( const path& lhs, const path& rhs ) noexcept; |
}} | }} | ||
− | {{dcl | num=6 | since=c++17 | 1= | + | {{dcl|num=6|since=c++17|1= |
− | bool operator>=( const path& lhs, const path& rhs ) noexcept; | + | bool operator>= |
+ | |||
+ | |||
+ | |||
+ | ( const path& lhs, const path& rhs ) noexcept; | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 24: | Line 28: | ||
Compares two paths lexicographically. | Compares two paths lexicographically. | ||
− | @1@ Checks whether {{ | + | @1@ Checks whether {{|lhs}} and {{|rhs}} are equal. Equivalent to {{c|!(lhs < rhs) && !(rhs < lhs)}}. |
− | @2@ Checks whether {{ | + | @2@ Checks whether {{|lhs}} and {{|rhs}} are not equal. Equivalent to {{c|!(lhs {{==}} rhs)}}. |
− | @3@ Checks whether {{ | + | @3@ Checks whether {{|lhs}} is less than {{|rhs}}. Equivalent to {{c|lhs.compare(rhs) < 0}}. |
− | @4@ Checks whether {{ | + | @4@ Checks whether {{|lhs}} is less than or equal to {{|rhs}}. Equivalent to {{c|!(rhs < lhs)}}. |
− | @5@ Checks whether {{ | + | @5@ Checks whether {{|lhs}} is greater than {{|rhs}}. Equivalent to {{c|rhs < lhs}}. |
− | @6@ Checks whether {{ | + | @6@ Checks whether {{|lhs}} is greater than or equal to {{|rhs}}. Equivalent to {{c|!(lhs < rhs) |
+ | |||
+ | }}. | ||
{{cpp/hidden friend|std::filesystem::path|plural=yes}} This prevents undesirable conversions in the presence of a {{c|using namespace std::filesystem;}} ''using-directive''. | {{cpp/hidden friend|std::filesystem::path|plural=yes}} This prevents undesirable conversions in the presence of a {{c|using namespace std::filesystem;}} ''using-directive''. | ||
+ | |||
+ | |||
+ | |||
+ | |||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | lhs, rhs | the paths to compare}} | + | {{par|lhs, rhs|the paths to compare}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | {{c|true}} if the corresponding comparison yields, {{c|false}} otherwise. | + | {{c|true}} if the corresponding comparison yields, {{c|false}} otherwise |
+ | . | ||
===Notes=== | ===Notes=== | ||
Line 51: | Line 62: | ||
In the case of equality, as determined by {{tt|operator{{==}}}}, only lexical representations are compared. Therefore, {{c|1=path("a") == path("b")}} is never {{c|true}}. | In the case of equality, as determined by {{tt|operator{{==}}}}, only lexical representations are compared. Therefore, {{c|1=path("a") == path("b")}} is never {{c|true}}. | ||
− | In the case of equivalence, as determined by {{ | + | In the case of equivalence, as determined by {{|filesystemequivalent}}, it is checked whether two paths ''resolve'' to the same file system object. Thus {{c|equivalent("a", "b")}} will return {{c|true}} if the paths resolve to the same file. |
+ | |||
+ | |||
+ | |||
===Defect reports=== | ===Defect reports=== | ||
Line 60: | Line 74: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/filesystem/path/dsc compare}} | + | {{dsc inc|cpp/filesystem/path/dsc compare |
+ | }} | ||
{{dsc end}} | {{dsc end}} | ||
− | {{langlinks|ja|zh}} | + | {{langlinks|ja|zh}} |
Latest revision as of 00:34, 16 August 2023
friend bool operator==( const path& lhs, const path& rhs ) noexcept; |
(1) | (since C++17) |
friend bool operator!=( const path& lhs, const path& rhs ) noexcept; |
(2) | (since C++17) (until C++20) |
friend bool operator<( const path& lhs, const path& rhs ) noexcept; |
(3) | (since C++17) (until C++20) |
friend bool operator<=( const path& lhs, const path& rhs ) noexcept; |
(4) | (since C++17) (until C++20) |
friend bool operator>( const path& lhs, const path& rhs ) noexcept; |
(5) | (since C++17) (until C++20) |
friend bool operator>=( const path& lhs, const path& rhs ) noexcept; |
(6) | (since C++17) (until C++20) |
friend std::strong_ordering operator<=>( const path& lhs, const path& rhs ) noexcept; |
(7) | (since C++20) |
Compares two paths lexicographically.
These functions are not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::filesystem::path is an associated class of the arguments. This prevents undesirable conversions in the presence of a using namespace std::filesystem; using-directive.
The |
(since C++20) |
Contents |
[edit] Parameters
lhs, rhs | - | the paths to compare |
[edit] Return value
[edit] Notes
Path equality and equivalence have different semantics.
In the case of equality, as determined by operator==
, only lexical representations are compared. Therefore, path("a") == path("b") is never true.
In the case of equivalence, as determined by std::filesystem::equivalent(), it is checked whether two paths resolve to the same file system object. Thus equivalent("a", "b") will return true if the paths resolve to the same file.
[edit] Example
This section is incomplete Reason: no example |
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3065 | C++17 | allowed comparison of everything convertible to path in the presence of a using-directive
|
made hidden friend |
[edit] See also
compares the lexical representations of two paths lexicographically (public member function) | |
(C++17) |
checks whether two paths refer to the same file system object (function) |