Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | filesystem‎ | path
m (Text replace - "{{noexcept" to "{{unreviewed noexcept")
m (fmt, {{c}})
 
(14 intermediate revisions by 5 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 );
+
bool operator==( const path& lhs, const path& rhs ) ;
 
}}
 
}}
{{dcl | num=2 | since=c++17 | 1=
+
{{dcl|num=2|since=c++17|1=
bool operator!=( const path& lhs, const path& rhs );
+
bool operator!=( const path& lhs, const path& rhs ) ;
 
}}
 
}}
{{dcl | num=3 | since=c++17 | 1=
+
{{dcl|num=3|since=c++17|1=
bool operator<( const path& lhs, const path& rhs );
+
bool operator<( const path& lhs, const path& rhs ) ;
 
}}
 
}}
{{dcl | num=4 | since=c++17 | 1=
+
{{dcl|num=4|since=c++17|1=
bool operator<=( const path& lhs, const path& rhs );
+
bool operator<=( const path& lhs, const path& rhs ) ;
 
}}
 
}}
{{dcl | num=5 | since=c++17 | 1=
+
{{dcl|num=5|since=c++17|1=
bool operator>( const path& lhs, const path& rhs );
+
bool operator>( const path& lhs, const path& rhs ) ;
 
}}
 
}}
{{dcl | num=6 | since=c++17 | 1=
+
{{dcl|num=6|since=c++17|1=
bool operator>=( const path& lhs, const path& rhs );
+
bool operator>=( const path& lhs, const path& rhs )
 +
 +
 +
 +
;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 24: 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)}}.
 +
 
 +
 +
 
 +
 +
 
 +
 +
 +
  
 
===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.
 
+
{{}}
===Exceptions===
+
{{unreviewed noexcept}}
+
  
 
===Notes===
 
===Notes===
Line 52: 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}}, 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 {{|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.
 +
 
 +
 +
 +
 
 +
 +
 +
 +
  
 
===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}}
 +
 +

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]