rcseacord / cmp_int Goto Github PK
View Code? Open in Web Editor NEWC implementation of C++ Utility functions https://en.cppreference.com/w/cpp/utility/intcmp
License: MIT License
C implementation of C++ Utility functions https://en.cppreference.com/w/cpp/utility/intcmp
License: MIT License
({ })
expressions are a gcc extension.
This is not necessary because all of that can be done in a single expression with nested ?:
conditional.
Claiming that this works for "integer types" is kind of weird because it seems that by design it doesn't work for char
and bool
. I am not questionning that design, but then this should be made cristal clear in the documentation.
The case listing in the macros is quite restrictive. I think better would be something like
#define is_signed(X) \
_Generic((X), \
bool: 0, \
char: (char)-1 < 0, \
signed char: 1, \
signed short 1, \
unsigned char: 0, \
unsigned short: 0, \
default: (0 ? (X)%1 : -1) < 0 \
)
That ensures to work also for __int128
or other such wild animals if they exist, and to give the expected answer for char
if the user has a concept of "could hold negative values" for being a signed type.
See: https://godbolt.org/z/YsdG8Wc7n
If you do:
cmp_equal(-5, (unsigned)5);
The result is a true value (or any case where the LHS is negative and the RHS is unsigned).
The probelm is that line 67((lhs) < 0 || make_unsigned(lhs) == (rhs) : \
should really be:
(lhs) >=0 && make_unsigned(lhs) == (rhs) : \
int x = 0;
puts(cmp_less(x++, 0U) ? "true" : "false");
printf("%d\n", x);
Outputs:
false
2
Which can be fixed using statement expression, but statement expressions are non-standard.
your code should stick to one single name prefix
besides the naming convention issue, this macro is superfluous and restricts the usage of your macros to the 6 standard integer types
The C++ variant has bool
return types. This should be the same, here. Just add a cast in front of the whole expression.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.