Following my series from less known features of Modern C++.

Modern C++: An Introduction
Swastik Baranwal ・ Dec 23 '19 ・ 3 min read
In Modern C++, you can also use and
, or
and not
as boolean operators which means &&
, ||
and !
respectively which is identical to languages like Python. They were in C but as macros but Modern C++ introduced them as keywords.
#include <iostream>
int main() {
std::cout << true and true << std::endl;
std::cout << true or false << std::endl;
std::cout << not true << std::endl;
return 0;
}
This is exactly identical to.
#include <iostream>
int main() {
std::cout << true && true << std::endl;
std::cout << true || false << std::endl;
std::cout << !true << std::endl;
return 0;
}
You can probably use any one of the styles depending on your codebase.
Top comments (4)
Why would they even do that? Even if you are not a pedantic preacher of the only-one-way-to-do-it philosophy, there should at least be a reason for having multiple ways to do it...
It's really disingenuous to label this "Modern" C++. It's a legacy C feature to support keyboards that didn't have all the previously accepted symbols (en.wikipedia.org/wiki/Digraphs_and...) or to make it more accessible for people with trouble inputting all the symbols.
Heh, that's funny. One of my colleagues once went on an angry tirade about how "modern" C++ was ruining programming by allowing you to have a one statement if block without any surrounding curly braces...
I didn't have the heart to tell him...
Interesting, I knew about trigraphs but apparently they went away in C++17.
and
isn't strictly a C language feature: it's a language keyword in C++ but is a macro in C.Here's more docs on the
and
/or
/not
alternative operators: en.cppreference.com/w/cpp/language...