3 ms·
Coming from C, none of this made sense to me. Wut is `foo() = default;`? If you want a default value of 0, why isn't it just struct foo { int a = 0; };
by frollogaston 1y ago
Coming from C, none of this made sense to me. Wut is `foo() = default;`? If you want a default value of 0, why isn't it just
struct foo {
int a = 0;
};
In Python, which is higher-level ofc, I still have to do `foo = 0`, nice and clear.
- zabzonk 1y ago> If you want a default value of 0, why isn't it ... It is.
- frollogaston 1y agoI know that works too, but there are also other unclear ways to do it.
- Maxatar 1y ago`foo() = default;` is an explicit way to generate a default constructor for `foo`. The default constructor works by recursively calling the default constructors for all class instance fields. In C++ there are a bunch of rules about when a class has a default constructor or not, but by explicitly declaring one you are guaranteed to have it so long as all your class instance fields have default constructors. Your example of having a field called `a` that is initialized to 0 is perfectly valid C++ as well but it's not the same as an explicitly declared default constructor.
- AlienRobot 1y agoYeah, this is obviously nonsensical. If the constructor is "default" then why do you need to explicit set it? Yeah, I know some objects don't have constructors, but it would make more sense if you had to explicit delete the default constructor, or the keyword "trivial" was used instead of default.
- frollogaston 1y agoIdk what other language has this "default" keyword. Seems unnecessary.
- motorest 1y ago> Coming from C, none of this made sense to me. Wut is `foo() = default;`? C does not have member functions, let alone special member functions such as constructors. It's understandable that someone with a C background who never had any experience using a language besides C would struggle with this sort of info. C++ improved upon C's developer experience by introducing the concept of special member functions. These are functions which the compiler conveniently generates for you when you write a simple class. This covers constructors (copy constructors and move constructors too). This is extremely convenient and eliminates the need for a ton of boilerplate code. C++ is also smart enough to know when not to write something it might surprise you. Thus, if you add anything to a basic class that would violate assumptions on how to generate default implementations for any of these special member functions, C++ simply backs off and doesn't define them. Now, just because you prevented C++ from automatically defining your constructors, that does not mean you don't want them without having to add your boilerplate code. Thus, C++ allows developers to define these special member functions using default implementations. That's what the default keyword is used for. Now, to me this sort of complaining just sounds like nitpicking. The whole purpose of special member functions and default implementations is to help developers avoid writing boilerplate code to have basic implementations of member functions you probably need anyway. For basic, predictable cases, C++ steps in and helps you out. If you prevent C++ from stepping in, it won't. Is this hard to understand? More baffling, you do not have to deal with these scenarios if you just declare and define the special member functions you actually want. This was exactly how this feature was designed to work. Is this too hard to follow or understand? I think the problem with C++ is that some people who are clearly talking out of ignorance feel the need to fabricate arguments about problems you will experience if you a) don't know what you are doing at all and aren't even interested in learning, b) you want to go way out of your way to nitpick about a tool you don't even use. Here we are, complaining about a keyword. If we go through the comments, most of the people doing the bulk of the whining don't even know what it means or how it's used. They seem to be invested in complaining about things they never learned about. Wild.
- frollogaston 1y agoYes it's too hard to follow or understand. Of all the langs I've used, C++ is the only one I can't trust to default-init things the expected way, and I use it every day.
- codr7 1y agoI agree in the example given. But add templates to the mix and a generic default becomes quite useful.