282 points · by eklavyaa · 9y ago · 173 comments
http://stackoverflow.com/questions/38060436/what-are-the-new...
std::optional and std::any are one of the good things. std::apply and std::invoke can help you write better functional c++17 code.
Anyway thanks for C++17. For sometime, I thought I will learn Rust.
On x86_64 Linux, it looks like a function with signature `void f(std::variant<int, char>)` expects its (8-byte) argument to be passed by reference, whereas `void f2(tagged_union_of_int_and_char)` passes its argument in rdi.
gcc (-O3) also generates miserable code for calling f(42):
4004f0: 48 83 ec 18 sub $0x18,%rsp
4004f4: 48 89 e7 mov %rsp,%rdi
4004f7: c7 04 24 2a 00 00 00 movl $0x2a,(%rsp)
4004fe: c6 44 24 04 00 movb $0x0,0x4(%rsp)
400503: e8 c8 ff ff ff callq 4004d0 <f>
400508: 48 83 c4 18 add $0x18,%rsp
40050c: c3 retq
as compared with calling f2(42): 400510: 48 bf 00 00 00 00 2a movabs $0x2a00000000,%rdi
400517: 00 00 00
40051a: eb c4 jmp 4004e0 <f2>
std::optional seems to have the same disease; the ABI is different from a plain struct containing a bool and the value and you get worse initialisation code. There's also a build time penalty to using std::optional; on my machine, including the code that uses optionals makes my trivial test program take 320ms to compile and link rather than 50ms.These look like yet more new C++ features that are worse than what they're trying to replace.
Now why it is not trivially copy constructible is another question. But the latest standard draft does not require it [variant.ctor] though it does require the destructor to be trivial if all type destructors are trivial [variant.dtor]. The GCC implementation also seems to ensure only trivial destructor not copy constructor (the code is here [1] if anyone likes to see).
EDIT: I did a small experiment that agrees with this - adding a copy constructor causes passing by pointer. https://godbolt.org/g/UG0YXJ
EDIT2: I filed a bug report for gcc, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80187
[1] https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-...
It should be possible to implement std::variant in such a way that it is trivially constructible and destructible as long as all elements are, but apparently stdlibc++ doesn't. This is GCC choice and other compilers apparently do differently. Unless GCC changes it before finalising their c++17 implementation, the choice will be unfortunately set in stone for them as they guarantee abi stability of the library.
P0602R0 is a proposal to require trivial copy/move (but strangely not destruct), don't know whether it has been accepted though.
In most cases, I imagine the performance penalty would be dwarfed by the cost of actually using the class.
void f(std::variant<int, char>)
would be f(x: IntOrChar)
roughly in Rust, I'm too lazy to make the types _exactly_ the same, but https://godbolt.org/g/pZde2A is roughly it.Actually, https://godbolt.org/g/1vcMeG might be even better to compare; I'm not an expert, but looks like the only difference is in the size.
last one, adding inline(never) so you can see the calls in main: https://godbolt.org/g/cVDjQH
movabs rdi, 21474836480
call example::f1@PLT
vs mov edi, 5
pop rbp
jmp example::f2@PLT
so, yup.In other words, the compiler can turn an Option into a null-pointer convention by reasoning from first principles.
(I think this optimization works for enums in general, and is somehow related to the `nonzero::NonZero` type, but I could be wrong about that.)
Example (look at the calls to f1 and f2 in example::main): https://godbolt.org/g/SSs6X2
(I know nothing about std::variant yet.)
This comes pretty close to what you're asking for I believe.
I faced the similar problem. I invested some money in Effective Modern C++(Scott Mayer). It covers the language part. I borrowed a book for C++11 concurrency. But, modern C++ lacks high-quality material at one place like Golang/Rust(the language guys have very good documentation, not boring and good enough to learn quickly). There are tons of scattered example.
I mean modern C++, with all lessons learned, all revisions thus far assumed to be supported, officially and unofficially deprecated coding styles marked in helpful tidy little "Don't do this and this is why!" red warning boxes. Bjarne Stroustrup himself said C++11... _11_ almost felt like a new language, and here we are already!
C++ is so big, and not only that, changing so much. I have a hard time catching up and a distilled "sum of knowledge" C++ programming language guide would be immensely helpful. C++17 including all its predecessors almost feel more like D at this point, soon enough only missing GC compared to that, which by itself have alternatives in modern C++ that some argue is even better.
I also think there's a disconnect in how people perceive C++ today. Many seem to think of an archaic language, Quora questions "Are C++ worthwile to learn today?" abound... Another argument for a high visibility language guide being useful?
Documents like these are helpful but not very easy to digest: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...
And I do consider, that some of the strongest criticism of C++, and that is, "that most people use a subset of it, so it feels like many different languages glued into one", is actually a strength .. it means you can ignore some the complexity .. until you want it, or need it
I am very happy, that C++ is adding new features every 3 years .. and I do plan to invest more time in C++
Complexity is good
And I think that's a major reason why C++ usage is gradually becoming a niche thing used only where absolutely necessary. As evidence for that claim I submit this chart (you need to scroll down a page): https://www.itjobswatch.co.uk/jobs/uk/c++.do
C++ used to be required for over 30% of all permanent programming jobs in the UK back in 2005. Now it's down to 8.5% and falling. That's in stark contrast with all the enthusiasm created by the features added since C++11, features that I very much welcome.
Programming is more accessible than ever before, more jobs require some element of programming than ever before, and more things are getting created using code. This means that people who have never written code before are writing code as a job of work, and are writing it in simpler-to-learn languages.
Newer, simpler programming jobs are acquiring programmers in other languages, not necessarily that C++'s complexity is driving people away from it into the arms of other languages or that C++ is not acquiring newer programmers at the same rates (on existing base) as before.
It would be interesting to know if the share of developers with formal training has risen or fallen in the past 12 years. I would have guessed it has risen in spite of a large number of JavaScript wielding web designers.
There used to be quite a lot of DIY programming using VB6, VBA, Excel, MS-Access, dBASE, FoxPro, etc, at a time when professional IT departments and cloud services were not as widespread as they are today. In my opinion these technologies were a lot more accessible than the current web platform.
As much as I complain about C++, I just don't think there's a viable alternative, and I somewhat reject the notion that writing performant code is a niche thing. Like, sure, there are common contexts where you can get away with wasting CPU, but IMO part of being a skilled professional programmer is getting the most out of the machine, and you just can't do that with most other languages.
It surely isn't, otherwise we wouldn't be getting wind back into going native sails, with Swift, Rust, Go, Crystal, D, .NET Native, CoreRT, Java 9 (just a little bit, more with 10)....
Fyi, when safety is important and C++ is the only viable alternative, there's SaferCPlusPlus[1].
And wrt new C++17 features, I'll just point out that the SaferCPlusPlus library provides a version of std::any (and std::variant) geared specifically for pointer/iterators. That is, you can, for example, dereference the contained pointer/iterator without needing to know its actual type. And for those that need it, it also provides the "recursive_shared_timed_mutex" that's notably missing from the standard library.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
If C++ is your only hammer then every problem looks like a nail to you. Of course, there are viable alternatives. At least Ada which is still used in avionics and other safety critical systems.
That's true if you work alone. If you work in a team, it's almost guaranteed that someone will love using advanced features just for the sake of using advanced features, creating insanely verbose and abstracted behemoths which are bad both for the human developers and the machines to read.
I suppose it's good for job security...
Having poorly thought out complexity and no complexity often result in similar ends.
Honestly, I've seen C++ code so muddled and obfuscated that I actually wondered if those devs just wanted to make themselves indispensable. I really dislike such an attitude.
This is also in my case anywyay a barrier to entry or at least a point of intimidation. Where do I start? Which subsets are essential? Which version should I start with if I am learning today?
There are some domains and industries that break from these guidelines (or similar subsets) for various reasons, but generally speaking this is the sensible approach to take.
[0]: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
I believe in exactly the opposite. I always try to follow the KISS principle (Keep it simple, stupid) whenever possible since it always turned out to be the most reliable and maintainable solution.
It is funny that the first Ada versions (93 and 95) were considered vastly more complex in comparison to C++ in those days. Today it's the opposite. Ada looks like a clean mature language which only adds features which are actually needed. C++ wants to copy everything, attempting to squeeze every new feature into its narrow syntactic framework while still suffering the legacy of C and old C++ for reason of compatibility.
The only reason (IMHO) to learn C++ today is for maintaining legacy code in business. In all other cases there are better typed languages - Ada, Rust, and Nim for instance.
C++ may look nice at the basic level. As soon as things get serious C++ devs have a tough time. How does the experienced C++ dev fight that? He looks into his long list of tips and nostrums which hie collected in all his years to avoid all the pitfalls and minefields of C++. I don't believe that will change significantly with C++17.
C++ needs a linter that rejects code that doesn't meet a certain standard ala Rubocop.
EDIT: In particular, the clang tidy linter has a way of checking your code against the C++ core guidelines [2], which is a set of modern best practices for the language.
I've never met a linter I liked more than a real compiler, not even close. Linters for scripting languages only exist because the scripting languages aren't compiled, and they are only lightweight second rate optional type checkers. Linters' attraction and usefulness is that you aren't required to use them, so you can iterate quickly and run code without having to wait for the compiler every single time you save your file. Linters are not better at keeping you within good style and safety bounds, generally speaking.
-Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wextra -Wformat=2 -Wimplicit-atomic-properties -Wmissing-declarations -Wno-long-long -Wnon-virtual-dtor -Woverlength-strings -Woverloaded-virtual -Wredundant-decls -Wreorder -Wswitch-default -Wunused-variable -Wwrite-strings -Wno-variadic-macros -Wno-unused-local-typedef -Wno-language-extension-token -fstrict-aliasing
and optionally: -Wconversion -Wdocumentation -Wfloat-equal -Wmissing-prototypes -Wold-style-cast -Wunreachable-code
and this is still insufficient. Why? Because this is only checking syntactical and semantic correctness. It can't check for violation of local policy, such as:no use of new/delete (use make_unique or make_shared)
correct and consistent use of virtual inheritance across the entire codebase
rule of three and rule of five usage
etc.
These are mostly higher-level concerns. Stuff which other people on my team might introduce, and/or miss in review.
I don't yet have anything automated in place for this, but clang-tidy is on my todo list for investigation.
Have you used any linters? Because linters are even worse than what you're describing with the C++ compiler. What they do is complain about style issues that some people believe are likely to lead to syntactical and semantic errors, without actually checking for the syntactical and semantic errors like a compiler does. Linters also don't check for violation of local policy, unless you extend them yourself. (And feel free to extend clang's warnings to suit your needs... it is open source.)
I fully 100% completely agree that C++ tooling could be way better, and I wish it was too. But that's not the issue we're talking about here at all.
Yes, a bare minimum standard, which must be backwards compatible. What a compiler enforces is not best practice, it is bare minimum. Something enforcing best practice has to be allowed to be backwards incompatible, since newer better ways of doing things will always come up (that's how languages evolve), and you want to discourage the older stuff.
"I've never met a linter I liked more than a real compiler" -- it's not an either or situation, use a linter on top of the compiler.
-Wall gives you a lot more than bare minimum. But, you can't really call it bare minimum when you compare compilers to linters in general. If a compiler enforces bare minimum, then a linter enforces less than bare minimum. Linters in general do not detect type errors that compilers catch and that prevent the program from running. A compiler is a bigger, better safety net than a linter.
I have ~25 years experience with many linters and many compilers, and my opinion is that compilers provide more security than linters, on the whole. When I can, I try to use a real compiler for production use of scripting languages, for example Google's closure compiler in JavaScript rather than linters like eslint or jslint.
> it's not an either or situation, use a linter on top of the compiler.
This is true, and I agree completely, it's not an either-or situation. That's in part what I was trying to say. The compiler already does linting and it does much better static analysis, so you don't need to choose, you can just use a compiler. There are certainly higher level style analyzers, clang-tidy was mentioned already, and there are others.
PEP-8, and Rubocop are in part a set of guidelines designed to prevent code mistakes and type errors precisely because the languages they represent don't come with a static type-checking compiler. Many of the linting rules and best practices only exist to prevent unsafe code that would have been prevented if there was a compiler involved, and only need preventing via style guides and linters because a compiler is not necessarily there.
There are certainly best social practices that some linters enforce that many compilers don't, but for that it doesn't make any sense to avoid learning C++. The OP's comment - that he feels like C++ is a minefield without a linter - is missing the forest. It is easier to know you're writing correct C++ than it is to know you're doing correct Python, because of C++'s compiler. Unfortunately no compilers or linters are that good at preventing you from writing code that runs and then crashes.
The same that micro-optimize every single line of code they write without ever running the code through a profiler.
If you mean the warnings, it's because they're way over the top.
switch(enabled)
> warning: switch on bool
switch(static_cast<int>(enabled))
Sigh.Now I know, "why would you ever switch on a bool?! It's only two possible states!"
Because it was in an ARM CPU emulator that decoded three to five fields per instruction, most of which were multiple bits long, but a few were just one bit. It gave a structured, consistent look to the code for each field to be implemented with a switch.
That's just one example, though. My current list of, "are you kidding me?" warning disables for Clang are:
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wempty-body"
#pragma clang diagnostic ignored "-Wparentheses"
#pragma clang diagnostic ignored "-Wreturn-type"
#pragma clang diagnostic ignored "-Wswitch"
#pragma clang diagnostic ignored "-Wswitch-bool"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wabsolute-value"
This was all for perfectly legal, unambiguous code. Anything even remotely suspicious I'd fix. You probably still think I'm a macho coder anyway, but I'm honestly not. I'm just rather obsessive about consistency and simplicity in my code.I would really not enjoy working on a codebase that forced me to use -Wall -Wextra -Werr.
>= 0 on an unsigned value is the classic one.
Matrix multiplication is super simple. Strassen is more complicated. Coppersmith–Winograd is brain bending (besides being slower on actual data and hardware).
The rule of thumb is: you have a complexity budget and a performance budget. Know what they should be, and choose economically.
Until of one the more secure alternatives gets more mainstream adoption, thus starting to be requested by our customers, C++ is going to be the tool to go to, when I need to step out of JVM and .NET languages to something more low level.
Something that could be enforced by all the compilers.
Many of the suggestions are supported in VC++ and clang-tidy.
Other commercial vendors are slowly adopting them on their linters.
Strangely, gcc seems absent from this effort.
So "std::byte" is not a character ("std::char") nor arithmetic ("std::int8_t/uint8_t"), but allows bitwise operations. It was really necessary? Anyone knows if that type allows defining the byte size in bits per instance?
That's not a purely hypothetical point; I already create custom types to do this. Not every 8-bit type is a character, nor is it necessarily an integer. I always found it frustrating that the default stream output was a character when using numerical quantities; now we can specialise raw output accordingly.
std::byte still seems pretty useless, though. There already is a built-in type for designating bytes: unsigned char.
They did much of the work needed to get modules, concepts, ranges, reflection, parallelism, etc ready to go, but this snapshot doesn't include them.
Nonetheless, many of those features will be finished soon and may be available by compilers before c++20
The coroutines, ranges and concepts TSs alone are making C++20 pretty interesting, and some of the features will be available in compilers before 2020.
"... Let's see Rust for what it is: the latest attempt at a C++ replacement. These have come and gone over the years, and it's by no means clear that Rust will succeed. As an example, Rust is currently 43rd of the 50 languages monitored in the Tiobe index. For comparison with languages of a similar vintage, Swift and Go are 10th and 17th respectively. C++ is 3rd. To put it bluntly: almost nobody is using Rust in the real world yet.
So please, ease up on the hype, Rust users. Have a little humility. You've got a nice little language with some really good ideas, and having a static analyser built into your compiler is cool. But there's a long way to go yet."
[1][1] https://www.reddit.com/r/cpp/comments/611811/have_you_used_r...
However, I think that post from Reddit was a bit misleading in its own way. Go has been much more mature from the beginning, given its authors and origins, and in some ways had an easier target, in being a slower but simpler, safer version of C, or a faster version of Python (as far as new users of the language are concerned). It's also in many ways sort of a modern version of C, so very familiar to potential users. Finally, it had its 1.0 release in 2012.
Rust, in contrast, had less of a head start. It is newer in a lot of ways in terms of its characteristics and features, and had a more difficult target, at the lower level and with the performance characteristics of C++. Finally, its 1.0 release wasn't until [less than] 2 years ago, in 2015. Go has been post-1.0 for over twice as long as Rust, which is a significant length of time in IT.
I think some people define systems programming as "interacting with the OS all the time"; which is something that Go is really good at.
Golang might be #4 or so; there might be some projects that use Rust but not much; the raw C that comes to mind is from third party libraries like libpcap.
In case you're curious, a lot of our internal tooling for monitoring (to provide an example) uses Python.
https://stackoverflow.com/insights/survey/2017/
This isn't exactly what you are looking for, but I think it still shows that C++ is fairly popular. C is second among systems languages at 19% and Go is near 4% (Rust is presumably less than 1.8% since it's not even on the list)
I believe that Rust has the power to take a significant part of C++ in coming years. Maybe that also Ada will increase due to the rise of public interest in code safety. C will remain as it is because there is no easier way to port system software to new platforms.
> GitHut is an attempt to visualize and explore the complexity of the universe of programming languages used across the repositories hosted on GitHub.
> Github is the largest code host in the world, with 3.4 million users. It's the place where the open-source development community offers access to most of its projects. By analyzing how languages are used in GitHub it is possible to understand the popularity of programming languages among developers and also to discover the unique characteristics of each language.
PS. Please don't forget to show some love for C2x now. :D
Clang [1] needs a couple more things.
MSVC [2] hasn't done as much, but shouldn't be too far away. [3].
Considering how long C++11 took before it was fully implemented, this seems so fast!
[0] https://gcc.gnu.org/projects/cxx-status.html#cxx1z
[1] http://clang.llvm.org/cxx_status.html
[2] https://msdn.microsoft.com/en-us/library/hh567368.aspx
[3] https://blogs.msdn.microsoft.com/vcblog/2016/01/22/vs-2015-u...
The security annex is worthless, as pointers and lengths are still used separately.
*Old but close to metal and fast and versatile.
And by making library code easier to write, understand, and maintain, we make it more likely that the more obvious features (like filesystem operations, database abstraction layers, new kinds of data structures, etc.) can be written and maintained.
All that being said, there are completely unecessary warts in C++ that need to be cleaned up. The best thing in-the-trenches C++ developers can do to help with this is to improve the software development lifecycle tooling and practices on the projects they are responsible for. Good automation, static analysis, and testing will make the C++ community more agile and more ready to do the much harder work of deprecating and deleting old technical debt. And it's good for the health of the project anyway.
Second, C++ is popular by pretty much every measure I could think of. Whether you want to measure lines of code, or number of projects, or number of developers - it is very high on the list.
It is true that is not popular with web developers and gets shouted down in those echo chambers, but the web entirely runs on C++ because all browsers are written in C++.
Not entirely true - Firefox is rewriting some components in Rust. They already included the first Rust code in Firefox 48, and Firefox 53 will require Rust to compile.
[0]: https://gist.github.com/mattico/a5e5f9d58a3991d0e3e844e97fa0...
Of course, I can install any compiler, but my employer might not allow me to use it. Porting to a new compiler is not an easy task when a code base is large; we postponed porting due to lack of time.
> Second, C++ is popular by pretty much every measure I could think of. Whether you want to measure lines of code, or number of projects, or number of developers - it is very high on the list.
More interesting measure would be the number of new projects.
> It is true that is not popular with web developers and gets shouted down in those echo chambers, but the web entirely runs on C++ because all browsers are written in C++.
In web backend C++ is not used. You find there C#, Java, PHP, Python, maybe Go.
Every now and then, I integrate a native library into our Java and .NET projects.
So it might not be popular, but any alternative, currently, means more work due to FFI layers, less tooling support regarding IDEs and debugging infrastructure.
We should support those more modern alternatives, however also be realistic that only in the last five years or so, many companies have started to move from C into C++ (e.g. BMW, Sony,...), and they will take a few decades to switch to something else.
I am glad I do not have to deal with C++, for the very reasons you mention, but as far as I can tell, it is fairly popular already.
C++ is still popular because there are a lot of existing projects; nobody would throw away their products. Many things were standardized too late, like threads or strings. Lack of standard solutions had led to produce custom ones, often proprietary. Nobody would rewrite working-for-years subsystem just because C++ finally learnt what a thread is. Another example is variety of incompatible build systems; I wish C++ had something similar to PyPi or CPAN (I even don't dream about Go-like import system). Not to mention incompatible compilers, but I must admit there is a progress.
C++ has many substantial problems and when I see that the committee wasting time on such unimportant things, like a 2D graphics library, it makes me upset.
The goal is not to replace SDL, rather provide a portable way of doing basic 2D graphics.
The C culture of leaving the libraries to the OS was one of the reasons why C++ got a chaos of interoperability issues, letting other languages that understood how that is relevant to gain mindshare in the late 90's.
C++ needs its batteries.
...despite claim that the light truck is a general purpose vehicle...people would chose for a new vehicle a bicycle, car, aircraft, motorcycle, or tank...
Use the right tools for the job. C++ is intended to be a general purpose systems programming language. An excellent tool for a wide variety of tasks, but not the be all and end all.
I am right now working on a library built in C++ and although I also have written some utility programs for the system in C++, its main interface is intended to be through (swig-enabled) Python and Common Lisp.
I wrote C++98/03 for years and kind of hated it. I love C++11/14. I've tried Rust and other languages, but C++ is such a good choice these days.
I agree that C++11 was a leap (older C++ was terrible). However, both C++14 and C++17 are just a sets of smaller or bigger improvements.