8 ms·
You can use `and` and its siblings instead of && and similar in C++11. Most people here, and in other boards, will try to convince you that it hurts readability
by Trollmann 5y ago
You can use `and` and its siblings instead of && and similar in C++11.
Most people here, and in other boards, will try to convince you that it hurts readability because 'that‘s how we always did it' (read I‘m used to it and don‘t like change).
- fouric 5y agoI am also of the opinion that `and` is more readable than `&&` (and isn't as easy to typo in a catastrophic way) - although my main point was about the weaker type system.
- qalmakka 5y agoC++ definitely hasn't a weaker type system than "newer" languages like Java - if any, it is much more richer and complex than most languages out there. What's happening here is a type conversion that has to be in place due to C not having a boolean type until 1999. C++ attempts to construct a boolean from the argument of an `if()`, and given that bool can be constructed from int, the conversion succeedes. You can define your own conversion operators to boolean, too, which are very useful for stuff like smart pointers and similar classes that may or may not have a value. struct A { std::string value; explicit operator bool() const { return this->value.size(); } }; // ... A a {}; if (!a) { // ... }
- fouric 5y ago"Not as weak as Java" is not a very interesting benchmark, as Java also has a poor type system. C++'s type system is pitiable relative to those of Rust, Haskell, OCaml, and SML. Moreover, in addition to being less expressive than them, C++'s type system is also weak, in formal sense, by allowing many implicit type conversions - which is one of the issues that I was complaining about. The fact that it "has to be in place due to C not having a boolean type until 1999" doesn't make it any less weak.
- jcelerier 5y agoWhich of "Rust, Haskell (core Haskell), OCaml, and SML" is able to parametrize types on values, à la template<auto> ?
- Macha 5y agoRust 1.51+: https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html
- comex 5y agoIn extremely limited form. That should change in the future, though.
- jcelerier 5y agoIt only supports integers so far, which c++ could do pretty much since before it was even standardized 25 years ago. Doesn't seem to support as wide of a type menagerie as C++20's NTTP. Does it even support parametrization over function pointers ?
- tialaramex 5y ago> It only supports integers so far Integer-like things. C++ char is similar to Rust's u8 or i8 but Rust's char and bool are very deliberately not just integers. > Does it even support parametrization over function pointers ? Can you give a clear example ? I think the answer is "No" but I struggled to put together an application for the feature I'm imagining. You would like to define a type, which is parameterised not by the type of function, but by specific functions? So, for example I can make a foo<sum> and a foo<average> and those are distinct types which presumably internally are using the provided function to behave differently? Except all the examples I think of come out better with just parametrisation over traits instead. So a clear example from you might illuminate whether this is a sizeable hole or just a difference of philosophy.
- 5y ago
- gavinray 5y agoI was surprised to read that `and`, `and_eq`, `xor`, etc are all supported "secondary/alternative operators" Never seen them used, but I use them in my C++ code when I have to write it to accomplish something else: https://en.cppreference.com/w/cpp/language/operator_alternative https://en.cppreference.com/w/cpp/language/operator_alternat...
- inetknght 5y agoI used to use them. Then I was burned by some Very Opinionated managers and coworkers who didn't like seeing new things. It wasn't a hill I was willing to die on; there's more important things to argue about in C++.
- oceanswave 5y agoDevelopment is a massive cargo cult
- josefx 5y agoThey have been around since the first standard and basically only exist as a hack around an ancient non ascii compatible encoding. So unless you got burned in 1995 nothing about them was new. They also aren't very consistent between C derived languages. Mostly because C# inherited its versions from VisualBasic, where And is a bitwise operator instead of a short circuiting one.
- inetknght 5y ago> * So unless you got burned in 1995 nothing about them was new.* I didn't mean to imply that they were new features to the language. I meant to imply that they were new style of coding to the manager & coworkers. Some other features that have burned me from managers & coworkers is `using` to change the visibility of parent class members & functions and CRTP.
- usefulcat 5y agoI use them, some of them anyway. I find them to be more readable (in particular’not’ instead of ‘!’). Also it means that I can reserve ‘&&’ for rvalue references.
- areyousure 5y agoWhile it is true that you can use `and` in C++11, that's a bit of an understatement: these keywords have in fact been present since the very first ISO C++ standard C++98!
- frankzinger 5y agoNote however that MSVC does not support these by default. You need to #include <ciso646> before their use, or you need to pass the `/permissive-` compiler option.
- account42 5y agoYou should be using /permissive- anyway unless you are dealing with a legacy codebase that breaks with that.
- Trollmann 5y agoTIL! Always thought this was a C++11 feature. Then this argument against the 'new' keywords is even weaker.
- yongjik 5y ago> that‘s how we always did it ... which, frankly, is a very good reason. Don't needlessly change something people are used to. Why is the blinker control on the left side and the wiper control on the right side? Because that's what people expect.
- geofft 5y agoOn the other hand, if it were actually the case that people kept turning on the wipers instead of signaling their turns, it would be a sign that we should figure out how to make these two different operations not use symmetrical levers, and that it would be okay to change people's expectations because those expectations weren't very firm. In aircraft, where it matters a whole lot more that you don't confuse the various levers, the handle of the landing gear lever is shaped like a little wheel and the handle of the flaps lever is shaped like a little wing edge: https://aviation.stackexchange.com/a/22689 https://aviation.stackexchange.com/a/22689 Typing "and"/"bitand" seems like the same sort of thing. It's a minor change, but it prevents errors.
- TeMPOraL 5y agoWish it was consistent, though. 'josefx is right in saying[0] these are still primarily hacks around encoding issues. | meaning | what is | what should have been | |---------+---------+-----------------------| | && | and | and | | &= | and_eq | bitand_eq | | & | bitand | bitand | | | | bitor | bitor | | ~ | compl | bitcompl | | ! | not | not | | != | not_eq | not_eq | | || | or | or | | |= | or_eq | bitor_eq | | ^ | xor | bitxor | | ^= | xor_eq | bitxor_eq | -- [0] - https://news.ycombinator.com/item?id=27928276 https://news.ycombinator.com/item?id=27928276
- unfamiliar 5y agoFyi it does in fact change depending on the car manufacturer.
- 5y ago
- asddubs 5y agoyou can even use it for rvalues!