Like almost all self-respecting projects, my own one also have macro to check some conditions in Debug mode:
#ifndef NDEBUG
# define DCHECK(x) if (!(x)) { ... }
#else
# define DCHECK(x)
#endif
But now I want to DCHECK()
some complex conditions, like file permissions:
...
auto has_permissions = [fd] {
struct stat st;
if (fstat(fd, &st) == 0) {
return (st.st_mode & (S_IRUSR | S_IWUSR)) == (S_IRUSR | S_IWUSR);
}
return false;
};
DCHECK(has_permissions());
...
Also, I don't want to pollute my code with lambdas which are not used anywhere besides a sole DCHECK()
, since in Release mode they will be marked as unused variables.
So the question is: is there a compact way to write DCHECK()
statement that declares, runs and checks any complex condition?
PS It's not a mandatory to use lambdas - it's just my proposal.
PPS DCHECK()
may be reimplemented, but all already existing invocations should still work without modifications.
Define the lambda within the DCHECK
macro and invoke it
DCHECK(([]{ return false; }()));
// ^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.