7 ms·
Are they trying to catch up to Rust while maintaining backwards compatibility? Or what? C++ needs the full definition of a class, including its private member
by Animats 9y ago
Are they trying to catch up to Rust while maintaining backwards compatibility? Or what?
C++ needs the full definition of a class, including its private members, before you can use its methods. This is a huge dependency headache. Most later languages have interfaces or traits or something to decouple class definition and interface definition. This looks like an attempt to deal with that. Of course, this is a retrofit, so it's going to be messy.
C++ has a lot of baggage. It may have exceeded its excess baggage allowance. The basic problem with C++ is that it has heavy encapsulation without memory safety. No other language has that. Either nothing is hidden, as with C, or the language is memory-safe, as is almost everything since C++.
- justicezyx 9y ago> full definition of a class Pointers and virtual class, why do you need the definition to use the interface? Could you provide more details? > The basic problem with C++ is that it has heavy encapsulation without memory safety. It's pretty simple to write memory safe code in c++, and encapsulation is a feature that not mandated in c++ code.
- adrianN 9y agoPointers and virtual classes have runtime overhead. It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases.
- pjmlp 9y agoIt is quite simple to write safe C++, when people don't treat the language as if it was a nicer C.
- adrianN 9y agoIf it were simple why is approximately nobody managing to do it? Do you think that they don't care?
- pjmlp 9y agoYep, they just don't care and keep throwing pointers instead references or std::vector/array, #define instead of const, char* instead of std::string and so on. I have been preaching how to write proper C++ code vs "nicer C" since the late 90's, but without much success, specially to enterprise developers. Many of the so called modern C++ features were already possible with C++98. As a good example, MFC 1.0 could have been like Turbo Vision, OWL, VCL, Qt and so on, but the test group from Microsoft found Afx too high level and wanted just a thin layer over Win16 thus it was reborn as MFC.
- seanwilson 9y ago> Yep, they just don't care and keep throwing pointers instead references or std::vector/array, #define instead of const, char* instead of std::string and so on. > I have been preaching how to write proper C++ code vs "nicer C" since the late 90's, but without much success, specially to enterprise developers. In no practical sense is it easy then. Even if you find it easy to write perfect code, you still have to work with other people that don't and use libraries which have problems. C++ has so many ways to shoot yourself in the foot I literally cannot think of a mainstream language that is actually more difficult to master. Would you say it was easy compared to Python, Java or JavaScript to write safe code in? Those languages have no undefined behaviour and they all have automatic memory management compared to C++.
- pjmlp 9y agoIt is harder yes, and actually nowadays I spend most of my time on Java/.NET, just diving into C++ for system stuff. However, the mechanisms for safer code are there, if one cares to use them in first place. The usual set warnings as errors, CI build with static analyzers that break on failure, unit tests with sanitizers that break on failure. But many companies don't care to set this workflow. They also don't care for doing that for Python, Java or JavaScript as well. JavaScript might have automatic memory management, but it has its own collection of WTF moments and implementation issues across browsers. Java is way better, yet there is a reason why Java Puzzles are so entertaining. While a tracing GC is much more productive than RAII/smart pointers, sometimes you also need to track down those references that prevent the collector to do its job. Android developers have lots of "fun" doing that regarding context and activities. One should not forget that safe languages only get us free from memory corruption, regarding safe code. There is still the whole spectrum of logical errors, specially bad in dynamic languages. In any case, until we finally get a mainstream OS written in Swift, .NET Native, Rust or D, we need to make the best use of our tooling for low level system coding activities and in this regard C++ is so much better than C.
- _pmf_ 9y ago> It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases. Using the STL after using Java collections or Qt collections is a shock. I think there really needs to be a "Safe STL" in addition to "Performant STL" that performs range checks. You simply cannot recommend using the STL for safe code.
- signa11 9y ago> I think there really needs to be a "Safe STL" in addition to "Performant STL" that performs range checks. indeed. for gcc, you can compile stuff with '-D_GLIBCXX_DEBUG' to enable random-iterator bound checks. moreover, random-access-iterators do have the 'at(...)' stuff available for bounds checked access to the containers. but, yeah, a standardized way would be very useful.
- pjmlp 9y agoVisual C++ on debug mode also enables iterator validation and bounds checking for arrays and strings. It is also possible to explicitly enable them in release code. https://blogs.msdn.microsoft.com/vcblog/2017/06/28/security-features-in-microsoft-visual-c/ https://blogs.msdn.microsoft.com/vcblog/2017/06/28/security-...
- duneroadrunner 9y ago> I think there really needs to be a "Safe STL" in addition to "Performant STL" that performs range checks. SaferCPlusPlus[1] provides compatible, memory-safe versions of the most commonly used STL containers. (The compiler switches mentioned by others add range checking, but do not, for example, catch use-after-free bugs.) > > It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases. Using SaferCPlusPlus, it now is simple to write memory-safe code in C++. And it's not much different from writing traditional (unsafe) C++ code. [1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus https://github.com/duneroadrunner/SaferCPlusPlus
- nicwilson 9y agoYou can't do that with C++'s "3-legged" algorithms. the begin - end - begin triplet is missing the second end.
- zvrba 9y ago> otherwise we would have a lot fewer vulnerabilities in C++ codebases. Are you sure you're not referring to _C_ codebases?
- adrianN 9y agoYes. All the popular browsers are mostly C++ for example.
- sideshowb 9y ago> Pointers and virtual classes have runtime overhead. Most popular languages don't allow putting objects on the stack and thus have at least comparable overhead anyway.
- justicezyx 9y ago> Pointers and virtual classes have runtime overhead. If you are sensitive to these overhead, you pretty much already only have C++ as your only choice. Or maybe Rust, but I do not know rust well to comment if Rust does not have such overhead. > It's not simple to write memory safe code in C++ No, in C++11 or newer, memory safe code is the default. That's the design goal of the newer standards of C++. IMHO, memory safety is no longer a central issue for C++. > a lot fewer vulnerabilities in C++ codebases. This only applies to legacy code.
- steveklabnik 9y agoRust doesn't have classes, and therefore doesn't have virtual classes. So it ends up being different. Dynamic dispatch is used pretty rarely in Rust. If you want dynamic dispatch, you don't use a virtual class, you use a "trait object". Basically it's a double pointer: a (pointer to vtable, pointer to data). Technically the C++ standard (as far as I know) doesn't define the exact implementation of this stuff, but in my understanding the vtable pointer is stored with the data. There are pros and cons to each approach.
- justicezyx 9y ago> If you want dynamic dispatch, you don't use a virtual class, you use a "trait object". Is this referring to Rust? Then it sounds like it will have similar overhead as C++'s virtual functions.
- steveklabnik 9y agoYes, it is, and yes, it does. There are small differences due to the stuff I said above, but it's similar.
- pjmlp 9y agoRust still needs to catch up with C++ in many areas, including HKT, value parameters in generics, compile time evaluation code, compilation speed at the same level as VS with incremental compilation and linking, GUI and mixed programming tooling across all major desktop OSes. Trying to use Gtk-rs made me see that lifetimes still need a bit of ergonomics improvements for any reasonable GUI programming in Rust. In fact some of the non-lexical lifetime improvements proposal are actually the exact problem I had related to callbacks handling. By the way, interfaces in C++ are pure abstract classes, only with pure virtual methods, without any private data.
- Longhanks 9y agoIs GUI programming in C++ still a thing, though? On Mac, you're better of with Swift/ObjC, on Windows, there's C#, and Gtk has binding to about any language. On mobile, C++ isn't even an option. The only thing coming to my mind is Qt, but development of Qt Widgets has basically stopped in favor of Qt Quick, and for cross platform GUIs there's other options like Xamarin or Electron (not that I'd prefer either).
- pjmlp 9y agoOn Windows you might have C#, but WPF and UWP lower layers are actually written in C++ and based on COM. Xamarin uses C and C++ to bind the nice .NET code into the platforms they support. Qt Quick is just the layout engine, many important parts of the stack keep being C++. Apple's Metal shaders are C++14. On Android Vulkan is only exposed to the NDK and SurfaceFlinger and hardware compositor are in C++. Plus if you compare compare Rust with Objective-C, Swift, C#, Java, the productivity story regarding GUI programming doesn't get better than when comparing with C++, on the contrary.
- ygra 9y ago> On Windows you might have C#, but WPF and UWP lower layers are actually written in C++ and based on COM. Well, this doesn't really influence the language programmers have to use to build applications, considering you can use C++, C#, VB.NET, or JavaScript to your heart's content. Windows GDI is a C API, but not every application using it is written in C.
- _pmf_ 9y ago> C++ needs the full definition of a class, including its private members, before you can use its methods. Is that true when using virtual methods from some virtual base class on an object created elsewhere? Is this problem solvable at all without specifying the ABI?
- panic 9y agoThis proposal isn't about adding interfaces to C++: it's about removing boilerplate through compile-time metaprogramming during the class definition process.
- yoklov 9y ago> The basic problem with C++ is that it has heavy encapsulation without memory safety. No other language has that. I think in general these things are unrelated, unless you stretch the definition of memory safety rather far. Also, you can hide things in C, just it's not always zero cost. Put private fields in a forward declared struct that's not implemented in any headers, use static for private functions (also outside of headers).
- misil 9y ago>catch up to Rust This proposal has nothing whatsoever to do about any Rust feature. It's about compile-time metaprogramming on steroids.
- steveklabnik 9y agoIt is similar to several Rust features, but has its differences, as you'd expect. They're also in-development features, so I agree that "catching up to Rust" feels like a bad way to frame this.
- petters 9y ago> C++ needs the full definition of a class, including its private members, before you can use its methods. This is a huge dependency headache. Most later languages have interfaces or traits or something to decouple class definition and interface definition. C++ has pure virtual classes, as well as the pimpl idiom. Both decouple the class definition from the interface. Yes, they have a theoretical runtime overhead. To get rid of that, you'd need a special "sizeof" member function instead of the list of private member variables. I guess that would be possible to introduce to save compilation times.
- taneq 9y ago> as well as the pimpl idiom To save others the arduous trial of googling it like I had to: http://wiki.c2.com/?PimplIdiom http://wiki.c2.com/?PimplIdiom In the past I've sometimes taken this a step further and defined a pure abstract class plus factory function in the header and then defined a derived implementation class in the source file, kind of a COM lite approach. (Is there an agreed name for this pattern? I don't doubt that, like most things I've invented, it was first thought of in the '60s or '70s. :P ) It's good for libraries because it separates all implementation details from the public interface while adding very little overhead for the library dev. Not so good for smaller utilities which are more natural as header-only classes, though. These days I don't bother with that kind of messing around until it actually becomes necessary, which it almost never does. I feel like my code's become way simpler over the years.
- Someone 9y ago"Is there an agreed name for this pattern?" I wouldn't know, but some Dylan book taught me that, along the abstract-concrete dimension, there are not two, but three kinds of classes: - concrete, instantiable - abstract, instantiable - abstract, not instantiable where a type <foo> is instantiable if a function make(<foo>) exists (because <foo> is abstract, that function must return an instance of a subclass of <foo>) That factory function makes your abstract class instantiable. Also, in Java, Foo would be an interface, and that factory would be a FooFactory.
- eej71 9y agoRather than specifically single it out as "trying to catch up to Rust", I'd say that C++ is doing what it has always been doing. Reaching for the future while maintaining backwards compatibility. It's been a great strategy for 30 years and I hope they keep at it.
- lallysingh 9y ago.. And that future is defined as "rust"
- dsacco 9y agoOnce again: there is plenty of room for both Rust and C++. One language does not need to "win." The Rust community doesn't endorse your flavor of evangelism here. It's not a holy war.
- pjmlp 9y agoExactly, and even if I complained about the current ergonomics of lifetimes, the amazing work of Rust developers is already influencing design decisions on Haskell, Swift, .NET, Pony and C++ regarding resource management.
- dsacco 9y agoAny chance you could throw up an email for me to contact you, or use the one in my profile? I'd be interested in chatting with you "offline" as it were.
- eej71 9y agoWhile your comment is a bit snarky, there is some truth to it in that if rust successfully evangelizes a technique, I'd expect the C++ world to bring it on board if there is room and I'm sure room can be found. The one topic to consider is this - is there a world where a language's existing code base is so large and the community so diverse that even _you_ would hesitate to break things for them? And at the same time, doesn't it make sense for that community to keep marching forward? Yes the language has baggage and the path forward isn't easy, but there are still paths ahead for C++. My advice to the rust community. Model yourself after the C++ strategy of evolution. Do not repeat the mistakes of the Python 2->3 world or the perl 5->6 world or what appears to be happening to Go. Don't break the past.
- saurik 9y ago> C++ needs the full definition of a class, including its private members, before you can use its methods. This is more a limitation of the ABI than the language (and even then, only comes up if you are using multiple inheritance or have private virtual functions). Objective-C had the same problem, and then they introduced the non-fragile ABI: no language change was needed, just a compile flag. (I do realize that there is some awkwardness in C++ if you attempt to declare these things in that private-decoupling sense; but to me the reason this is most brutal is not hiding of state, where we have solutions already, but being able to add and remove members without having to recompile users.)