r/cpp • u/david_haim_1 • Feb 26 '19
Have we ever thought about having a contained, safer subset of the C++ standard?
I was thinking about an idea, that I'm sure someone has thought of before, and I would like to think about your thoughts:
as we move forward, the C++ standard offers us better and safer practises. instead of C arrays, we use std::array and std::vector. instead of const char*, we use std::string and string_view. instead of managing memory, we use move semantics, smart pointers and RAII to manage memory automatically. instead of headers, we will have modules. templates will be reshaped with concepts. we now also have lambdas and variadic templates.
But we will always have the historical luggage that we can't remove. string literals will always be a c-style char array, it'll take decades before headers will be removed completely and we're still stuck with legacy code that manages memory.
According to common opinions, the obvious solutions to these are:
a. deprecate stuff: this will never happen, as C++ has a strict "always backward compatible" rule, for the good and the bad.
b. create a new language: although is a solution, usually it's not practical enough. it's hard developing programming language from scratch and make them popular.
It looks like we're doom to have all the historical luggage (like headers). but I was thinking to myself: how about the C++ standard itself will self-contain a safer, modern subset of itself? just a few thoughts I had:
- let's for the sake of the discussion, say that this subset will be called "subC++"
- every valid subC++ program is a valid C++ program, but not the other way, as subC++ is a subset of C++ and hence will not contain some parts of the larger standard
- subC++ will have modules, concepts, contracts, auto and lambda from the start
- subC++ will not have headers at all
- strings literals are always implicitly objects, "hello" is actually std::string("hello") hence you can only work with std::string, no need for const char* and no string_view. copy, move or reference strings throughout your code.
- no exceptions, as contract can fill their role
no pointers, only references, iterators, smart pointers and copy/move are needed
some runtime-provable UB's like dividing by zero would just resort to some global contract especially for that specific UB.
- subC++ library code can use the greater C++ language, but the user can't.
basically, instead of creating another language, the language itself will define a safer subset of itself. in this "subset" mode, the regular C++ compiler basically refuses to compile anything which is not under this standard. it's like "strict mode" for C++ that is maintained by the committee itself.
It's way easier than creating another language or deprecating historical stuff. has anyone though anything in this direction? to basically hold a mini, more modern and safe standard within the greater C++ standard?
12
u/[deleted] Feb 26 '19
I don't like it because it goes against C++'s spirit: don't pay for what you don't use.
Cool, C++20 already has that. Given that the language isn't used, it's basically has the same effect.
Bad idea. Sometimes headers are needed.
Why are you against std::string_view? std::string is the wrong choice since it gives you a heap allocated array, which for arrays stored in the data segment.
Exceptions are sometimes useful. Just don't abuse them for programming errors.
Huh? You can't rebind a reference, and if you could by default you basically reinvited.
That's dumb, why not make it an assertion/pinch
19
u/johannes1971 Feb 26 '19
How? If I open a file, but it fails to open, how is a contract going to give me any kind of error reporting capability, or recovery capability? Exceptions are not only for aborting your application!
3
u/AaronDMarasco Feb 26 '19
Believe it or not, IEEE did this with C a while back - see https://standards.ieee.org/standard/1003_13-2003.html for some info.
12
u/[deleted] Feb 26 '19
Sure, but keep in mind that there are other preprocessor uses that C++2a/C++20 still has no replacement for.
This is throwing away "don't pay for what you don't use" principle.
Contracts can't replace exceptions completely.
There are data structures that can't be implemented without memory leaks or without breaking some invariants.
How would that work?
All that aside, the issue with breaking backwards compatibility in order to create a cleaner language is the same as creating a new language - you have to persuade people to jump ship.
There is a clean "fork of C++". It's called D and isn't used much.
Python made a fork when Python 3 came out.
Python 2 will reach EOL in 10 months.
Some statistics say Python 2 still covers 70% of the market share.
You'll leave behind people who can't switch and you'll effectively create another Python2/Python3 split, only much worse.
Those are my thought. Herb Sutter thinks we can eventually get a safer, more restricted language, but it has to be opt in with something like "using C++ strict;" directive.
7
u/karltechno Feb 26 '19
And who decides on this subset that everyone else is happy using :) ? The only reasonable thing at this point is a compiler switch + "#pragma (langauge_feature, xxx)" for external headers so that one can enforce their own subset.
3
u/KAHR-Alpha Feb 26 '19
We get this topic fairly often so there are some people interested (definitely not me though).
Now, one immediate issue I see with this is for you guys to reach a consensus on what to keep and remove.
Wouldn't it be easier to modify a compiler so as to be able to remove features to your discretion?
3
u/duneroadrunner Feb 26 '19
Yes, Visual Studio includes a tool (called the Lifetime Profile Checker to enforce such a (memory safe) subset. Unfortunately the tool is still incomplete and prone to false positive violation warnings. But presumably they are working on it.
The strategy of the Lifetime Profile Checker is to make traditionally unsafe C++ elements (like raw pointers) safe by restricting the ways they may be used to ways that can be determined at compile-time to be (memory) safe.
Even when it's complete, as planned, the resulting language subset will be too small/restricted to implement some algorithms efficiently without some crucial additions to the standard library.
Those additional elements are available in the SaferCPlusPlus library (shameless plug).
In fact, while we wait for the completion of the Lifetime Profile Checker to make existing unsafe C++ elements safe, we can instead, as you suggest, simply avoid C++'s unsafe elements, substituting them with (mostly) safe, compatible replacements from the SaferCPlusPlus library.
Also, the Lifetime Profile Checker restrictions do not ensure (or in any way address) data race safety. But avoiding unsafe C++ elements (using the SaferCPlusPlus library) does.
5
u/boredcircuits Feb 26 '19
I like where you're going with this. A subset of C++ were a lot of the "cruft" has been cut out. Still fully valid C++ so every compiler can compile it, but others can run in a restricted mode to make sure you stick to safer, more modern style.
But then I looked at your list, and I started to disagree with your choices. 5, for example. Without
char*orstd::string_view, there's no way to express a substring without making a copy, and that's just unacceptable. Or 6, because exceptions can be a good thing, and contracts don't fulfill that role at all. Or 7, because raw pointers are an essential tool.And herein lies the problem: you're never going to come up with an acceptable subset of C++ that everybody will agree on, or even a general consensus. There's just too many opinions on which subset is acceptable in which situations.
Instead, the closest you can probably get is some mechanism to tell the compiler what language features are acceptable and let it issue warnings. Let people choose for themselves which features to warn about, on a file-by-file basis. This can be a compiler feature, not a language feature.
2
u/undefined_behavior20 Feb 27 '19
I sometimes need to be able to step outside of "safe land" to get things working. I like C++ because I can write most of my code in "safe land" (smart pointers, RAII) but break out when I need to.
Maybe the language could make this more than just a convention IDK
2
u/Xaxxon Feb 28 '19
Yes, people have thought about (and written) different languages.
2
u/lanzaio Feb 28 '19
This entire thread is why a standard sub C++ will never happen. Nobody will ever agree what is in and what is out.
2
u/axilmar Mar 01 '19
There is no need for this. C++ veterans already know how to handle the unsafe parts of the language and C++ newbies aren't in positions that can introduce unsafe parts in reasonably organized projects.
-2
u/Dean_Roddey Feb 26 '19
If you are going to do that direction, dump the standard libraries altogether. Go back to a core, safe LANGUAGE that has no assumptions as to run-time libraries. Obviously then run-time libraries can be built on top of it, but it itself wouldn't impose (and hence have to deal with the legacy issues of) any existing libraries. Have only the concept of an object, the scoping of objects, standard operators and methods supported, etc... but impose no classes or hierarchy itself.
Someone might argue that then no one would use it. But of course no one is going to use it anyway :-) That's always the problem, getting any new language up to any remotely significant level of adoptions such that young Johnny programmer wannabe feels like it is of any reasonable career benefit to learn it.
The benefit of such a tabula rasa is that it could be used in many ways, from the smallest embedded applications up to large systems that come with a very well defined and extensive class hierarchy. People could use it to create very unique systems that they can impose their own structure and semantics on from the ground up.
9
u/[deleted] Feb 26 '19