https://github.com/htfy96/cpp-cmp
Header-only library providing smart `cmp` function for C++11
https://github.com/htfy96/cpp-cmp
Last synced: about 10 hours ago
JSON representation
Header-only library providing smart `cmp` function for C++11
- Host: GitHub
- URL: https://github.com/htfy96/cpp-cmp
- Owner: htfy96
- License: apache-2.0
- Created: 2016-06-01T15:42:53.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-02T04:10:11.000Z (about 10 years ago)
- Last Synced: 2025-10-27T11:44:13.513Z (8 months ago)
- Language: C++
- Size: 21.5 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cpp-cmp 
Header-only library providing smart `cmp` function for C++11.
## Usage
```cpp
#include "cmp.hpp"
```
```cpp
using namespace cppcmp;
// cppcmp::cmp
assert(-1 == cmp(-3, 5));
//assert(1 == cmp(100L, '\0')); // must be of the same type
assert(-1 == cmp( std::forward_as_tuple(std::string("AAA"), 20),
std::forward_as_tuple(std::string("AAA"), 30)
));
std::string x("abcd");
assert(0 == cmp( std::forward_as_tuple(x.size(), x),
std::forward_as_tuple(x.size(), x)
));
struct user_defined_type
{
// ...
// User defined type could provide < and ==
bool operator<(const user_defined_type& other) const
{
return x_ < other.x_;
}
bool operator==(const user_defined_type& other) const
{
return x_ == other.x_;
}
};
user_defined_type a(23), b(16);
assert(1 == cmp(a, b));
struct user_defined_type_cmp
{
// ...
// you can also provide 'cmp' member function
int cmp(const user_defined_type_cmp& other) const
{
if (x_ == other.x_) return 0;
return x_ < other.x_ ? -1 : 1;
}
};
user_defined_type_cmp c(-10), d(42);
assert(-1, cmp(c, d));
namespace cppcmp
{
// Also, you can directly provide cmp in cppcmp namespace
int cmp(const vector& a, const vector& b)
{
return cmp(a.size(), b.size());
}
}
```
### Precedence
overrided `cppcmp::cmp` > member function `cmp` > `operator <` + `operator ==`
## License
Apache License 2.0