Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/filesystem/path/operator cmp"

From cppreference.com
< cpp‎ | filesystem‎ | path
m (+ {{todo: example}})
m (fmt, {{c}})
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{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=
 
friend bool operator==( const path& lhs, const path& rhs ) noexcept;
 
friend bool operator==( const path& lhs, const path& rhs ) noexcept;
 
}}
 
}}
{{dcl | num=2 | since=c++17 | until=c++20 | 1=
+
{{dcl|num=2|since=c++17|until=c++20|1=
 
friend bool operator!=( const path& lhs, const path& rhs ) noexcept;
 
friend bool operator!=( const path& lhs, const path& rhs ) noexcept;
 
}}
 
}}
{{dcl | num=3 | since=c++17 | until=c++20 | 1=
+
{{dcl|num=3|since=c++17|until=c++20|1=
 
friend bool operator<( const path& lhs, const path& rhs ) noexcept;
 
friend bool operator<( const path& lhs, const path& rhs ) noexcept;
 
}}
 
}}
{{dcl | num=4 | since=c++17 | until=c++20 | 1=
+
{{dcl|num=4|since=c++17|until=c++20|1=
 
friend bool operator<=( const path& lhs, const path& rhs ) noexcept;
 
friend bool operator<=( const path& lhs, const path& rhs ) noexcept;
 
}}
 
}}
{{dcl | num=5 | since=c++17 | until=c++20 | 1=
+
{{dcl|num=5|since=c++17|until=c++20|1=
 
friend bool operator>( const path& lhs, const path& rhs ) noexcept;
 
friend bool operator>( const path& lhs, const path& rhs ) noexcept;
 
}}
 
}}
{{dcl | num=6 | since=c++17 | until=c++20 | 1=
+
{{dcl|num=6|since=c++17|until=c++20|1=
 
friend bool operator>=( const path& lhs, const path& rhs ) noexcept;
 
friend bool operator>=( const path& lhs, const path& rhs ) noexcept;
 
}}
 
}}
{{dcl | num=7 | since=c++20 | 1=
+
{{dcl|num=7|since=c++20|1=
 
friend std::strong_ordering
 
friend std::strong_ordering
 
     operator<=>( const path& lhs, const path& rhs ) noexcept;
 
     operator<=>( const path& lhs, const path& rhs ) noexcept;
Line 28: Line 28:
 
Compares two paths lexicographically.
 
Compares two paths lexicographically.
  
@1@ Checks whether {{tt|lhs}} and {{tt|rhs}} are equal. Equivalent to {{c|!(lhs < rhs) && !(rhs < lhs)}}.
+
@1@ Checks whether {{|lhs}} and {{|rhs}} are equal. Equivalent to {{c|!(lhs < rhs) && !(rhs < lhs)}}.
  
@2@ Checks whether {{tt|lhs}} and {{tt|rhs}} are not equal. Equivalent to {{c|!(lhs {{==}} rhs)}}.
+
@2@ Checks whether {{|lhs}} and {{|rhs}} are not equal. Equivalent to {{c|!(lhs {{==}} rhs)}}.
  
@3@ Checks whether {{tt|lhs}} is less than {{tt|rhs}}. Equivalent to {{c|lhs.compare(rhs) < 0}}.
+
@3@ Checks whether {{|lhs}} is less than {{|rhs}}. Equivalent to {{c|lhs.compare(rhs) < 0}}.
  
@4@ Checks whether {{tt|lhs}} is less than or equal to {{tt|rhs}}. Equivalent to {{c|!(rhs < lhs)}}.
+
@4@ Checks whether {{|lhs}} is less than or equal to {{|rhs}}. Equivalent to {{c|!(rhs < lhs)}}.
  
@5@ Checks whether {{tt|lhs}} is greater than {{tt|rhs}}. Equivalent to {{c|rhs < lhs}}.
+
@5@ Checks whether {{|lhs}} is greater than {{|rhs}}. Equivalent to {{c|rhs < lhs}}.
  
@6@ Checks whether {{tt|lhs}} is greater than or equal to {{tt|rhs}}. Equivalent to {{c|!(lhs < rhs)}}.
+
@6@ Checks whether {{|lhs}} is greater than or equal to {{|rhs}}. Equivalent to {{c|!(lhs < rhs)}}.
  
@7@ Obtains the three-way comparison result of {{tt|lhs}} and {{tt|rhs}}. Equivalent to {{c|1=lhs.compare(rhs) <=> 0}}.
+
@7@ Obtains the three-way comparison result of {{|lhs}} and {{|rhs}}. Equivalent to {{c|1=lhs.compare(rhs) <=> 0}}.
  
 
{{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''.
  
{{rrev | since=c++20 |
+
{{rrev|since=c++20|
 
{{cpp/note synthesized compare}}
 
{{cpp/note synthesized compare}}
 
}}
 
}}
Line 50: Line 50:
 
===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===
 
@1-6@ {{c|true}} if the corresponding comparison yields, {{c|false}} otherwise.
 
@1-6@ {{c|true}} if the corresponding comparison yields, {{c|false}} otherwise.
@7@ {{c|std::strong_ordering::less}} if {{tt|lhs}} is less than {{tt|rhs}}, otherwise {{c|std::strong_ordering::greater}} if {{tt|rhs}} is less than {{tt|lhs}}, otherwise {{c|std::strong_ordering::equal}}.
+
@7@ {{c|std::strong_ordering::less}} if {{|lhs}} is less than {{|rhs}}, otherwise {{c|std::strong_ordering::greater}} if {{|rhs}} is less than {{|lhs}}, otherwise {{c|std::strong_ordering::equal}}.
  
 
===Notes===
 
===Notes===
Line 62: 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 {{ltf|cpp/filesystem/equivalent|filesystem::equivalent}}, 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.
+
In the case of equivalence, as determined by {{|filesystem::equivalent}}, 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.
  
 
===Example===
 
===Example===
Line 74: 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|es|ja|ru|zh}}
 
{{langlinks|es|ja|ru|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.

1) Checks whether lhs and rhs are equal. Equivalent to !(lhs < rhs) && !(rhs < lhs).
2) Checks whether lhs and rhs are not equal. Equivalent to !(lhs == rhs).
3) Checks whether lhs is less than rhs. Equivalent to lhs.compare(rhs) < 0.
4) Checks whether lhs is less than or equal to rhs. Equivalent to !(rhs < lhs).
5) Checks whether lhs is greater than rhs. Equivalent to rhs < lhs.
6) Checks whether lhs is greater than or equal to rhs. Equivalent to !(lhs < rhs).
7) Obtains the three-way comparison result of lhs and rhs. Equivalent to lhs.compare(rhs) <=> 0.

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 <, <=, >, >=, and != operators are synthesized from operator<=> and operator== respectively.

(since C++20)

Contents

[edit] Parameters

lhs, rhs - the paths to compare

[edit] Return value

1-6) true if the corresponding comparison yields, false otherwise.
7) std::strong_ordering::less if lhs is less than rhs, otherwise std::strong_ordering::greater if rhs is less than lhs, otherwise std::strong_ordering::equal.

[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

[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) [edit]
checks whether two paths refer to the same file system object
(function) [edit]