r/cpp • • Mar 19 '20

Why so many students are using '#include<bits/stdc++.h>' instead of '#include<iostream>'?

I am hiring for my company. And the codes submitted for a test problem has '#include<bits/stdc++.h>' at the beginning. The MacOS default compiler complains

main.cpp:1:9: fatal error: 'bits/stdc++.h' file not found

#include<bits/stdc++.h>

^~~~~~~~~~~~~~~

1 error generated.

Why people are using '#include<bits/stdc++.h>'?

225 Upvotes

163 comments sorted by

View all comments

185

u/[deleted] Mar 19 '20

Because they are mostly folks who are coming in via competitive programming backgrounds and/or most of the C++ stuff they saw comes from websites like geeksforgeeks, where other competitive programmers write algorithm tutorials in C++ using patterns like this.

I have said it once and I have said it before, if a candidate's exposure to C++ is only through competitive programming or leetcode, then their C++ experience is not to be taken seriously. They may know some things about the STL, their concepts like OO, templates, move semantics, memory management, concurrency, build systems, testing i.e stuff that actually matters in a production C++ environment, are seriously lacking or non-existent.

I took a guess that you're from India, and well, looks like you are, this is very prevalent there.

56

u/jangid Mar 19 '20

Yes. This is a big problem here. People solve competitive programming problems but missing heavily on the conceptual parts.

-12

u/taxeee Mar 19 '20 edited Mar 19 '20

It may be partly your fault here. If you want to test candidates in conceptual parts, do not ask them to balance a binary tree. Ask them to implement perfect forwarding manually.

Edit: Implementing perfect forwarding off memory may be a bad example, but I stand by my point. Testing X when you need Y is pointless.

17

u/[deleted] Mar 19 '20

I'd much rather hire someone who can balance a binary tree than someone who knows how to implement perfect forwarding manually.

One is a timeless concept that is fundamental to computer science and transfers to a variety of different languages and problem domains, the other is C++ minutiae that is not only a kind of hack to work around the limitations of C++ as a language, but even within the C++ community is only relevant to a tiny subset of C++ developers.

6

u/mredding Mar 19 '20

I'd like to argue the opposite.

Balancing a binary tree is so very timeless I don't want to implement my own, nor do I ever want to see anyone else do it. There are plenty of existing implementations of binary trees out there in popular and well maintained libraries. To write your own is just duplicating work, a condition of not-invented-here syndrome, and error prone compared to a proven and pre-existing solution. If I'm not in the business of selling binary trees, I don't want to make them. You would have to make an outstanding case to write one in house, and I would argue if you did have a legitimate case, you could use it as the basis of a masters thesis, and whatever you're doing should be published.

On the contrary, perfect forwarding is a new concept with big benefits, and I need people who intuitively understand those concepts and find their use in their code regular. Ours is an old language, that it and its community are showing their age and resistance to change. I find junior developers who understand perfect forwarding and other modern C++ concepts have advantages writing tomorrow's C++ code over all the seasoned veterans who don't understand these things - and in my experience, won't.

And if you would argue those are mediocre and bad developers, I work with a guy who co-authored Windows 95 and Direct X, I work with a guy who maintained Intel's FORTRAN compiler for 15 years and is actually the first guy to partial sample audio synthesis, the basis of all modern synthesized instruments. People in my office have chops, and I can't get them to pick up on this stuff.

10

u/[deleted] Mar 19 '20

Balancing a binary tree is so very timeless I don't want to implement my own, nor do I ever want to see anyone else do it.

No one is asking you to implement your own, you're being asked whether you understand it as a concept. I think it's shameful that many professionals in this field don't actually understand the fundamentals and take it for granted, seeing this field as nothing more than an exercise in gluing together a series of frameworks. Furthermore are you suggesting that you want people to implement their own perfect forwarding when std::forward exists? If you're not suggesting that, then this entire paragraph seems moot.

On the contrary, perfect forwarding is a new concept with big benefits, and I need people who intuitively understand those concepts and find their use in their code regular.

It's not at a concept at all, it's specific C++'ism due to a syntactic quirk of the language. This is about as concrete as it gets and its inclusion in C++11 was a mistake. No other language would ever introduce something as atrocious and error prone as T&&. Furthermore the actual concept of move semantics and forwarding predates C++11 by decades and has been put to practice far more elegantly in more modern languages like Rust. C++ shot itself in the foot on this issue, quite possibly because of a lack of understanding of the concepts involved.

Furthermore, instead of talking down about people with 15 or more years of experience who are skeptical of the huge increase in unnecessary complexity that C++ has introduced, maybe actually try to understand their point of view. Have a look at Mike Acton's talk about the alternative approaches to designing software that is a lot more "old-school" than today's fascination with C++ metaprogramming.

When I was a younger developer I loved all of C++'s complexity and I loved learning it, I thought it made me smart knowing all the corner cases of the language, memorizing entire sections of the standard, and using template metaprogramming to its max. But honestly after 20 years of this and looking back, I feel now like none of that minutiae really made me a good programmer even though it sure made me feel like one.

Nowadays I value simple, straight forward code, and while I still do use C++, the C++ I use is very minimal in terms of features and complexity. If I have a recommendation for becoming a more valuable developer, I'd say prefer learning various types of algorithms (greedy algorithms, sorting and searching, dynamic programming, etc...), have a good grasp for how to structure an algorithm so that you can prove properties about it including its performance characteristics and its invariants.

That kind of knowledge will stay with you for decades and really help guide your ability to write clean, efficient code... knowing some quirky aspect like how to implement perfect forwarding or using various compiler hacks like SFINAE is stuff that looks smart and useful when you first learn it, but it gets dated real fast and quickly makes your code devolve into a Turing tarpit.

1

u/pjmlp Mar 19 '20

When I was a younger developer I loved all of C++'s complexity and I loved learning it,

Me too, it was the perfect next language to move on after Object Pascal with Borland compilers.

Nowadays I just hope that my managed languages eventually improve to the point, that the occasional C++ native library no longer is required, beyond providing C like bindings (or COM/UWP ones) for language interoperability.

I would never managed to go through interview coding questions about modern C++ minutia.

-1

u/[deleted] Mar 19 '20

[removed] — view removed comment

2

u/[deleted] Mar 19 '20

There is absolutely no legacy property of the language that explains the mistake I mentioned, namely:

template<typename T>
void f(T&& v);

The parameter v should have always resolved into an rvalue reference of type T, case closed. The standard's committee decided to be cute and instead of applying that simple principle, decided that v is sometimes an rvalue reference, and sometimes an lvalue reference which has done nothing but create an absolute mess and resulted in significant loss of productivity.

There are numerous other proposals that are broken that have nothing to do with legacy requirements either, such as initialization:

auto v = std::vector{1, 2};

What legacy was maintained by using intializer syntax in this ambiguous way when the following syntax would have been unambiguous?

auto v = std::vector{{1, 2}};

Now it's impossible to use initializer syntax in generic code:

auto v = T{1, 2};

Will that invoke T's constructor taking two int's or will it invoke T's constructor taking an std::initializer_list? You just don't know and we're now stuck with this syntactic failure forever.

1

u/[deleted] Mar 19 '20

[removed] — view removed comment

3

u/Fureeish Mar 19 '20

That's not the point. The point is that they could've chosen a different identifier for forwarding reference instead of reusing && and introducing inconsistencies.

→ More replies (0)

2

u/evaned Mar 19 '20

There are plenty of existing implementations of binary trees out there in popular and well maintained libraries. To write your own is just duplicating work, a condition of not-invented-here syndrome, and error prone compared to a proven and pre-existing solution.

To play devil's advocate here, what library would you suggest that the company I work for should have used for an applicative map?

Nowadays I know of immer, but at least from a quick search I don't see anything else that I'd have any more trust in than our code... and unless immer comes with a time machine it was more than a decade too late on the scene. So if you were to work with us, you might wind up needing to go look into our in-house tree.

On the contrary, perfect forwarding is a new concept with big benefits, and I need people who intuitively understand those concepts and find their use in their code regular.

And also FWIW, while I wouldn't consider myself proficient in modern C++, I do actively follow its developments and have done some coding in it. I think I am fairly comfortable in using stuff like std::forward to carry out perfect forwarding, and can explain why you would want to do it and what benefits it would bring.

I am not confident that I could implement it without looking it up. I have vague memories that it might actually be the same as std::move under the covers, but I don't know if that's just at the level of approximation that std::move is almost just static_cast<whatever&&> and maybe there are some differences.

I'm with sakarii. Given two candidates if all I know about them is candidate A correctly balanced the tree and candidate B correctly wrote std::forward, from a Bayesian perspective I'm more likely to like candidate A.

1

u/taxeee Mar 19 '20

Your co-workers seem really experienced (and I assume you are too, since you work with them). Any hint on what kind of industry you are in?

1

u/mredding Mar 19 '20

Databases, virtualization, and cloud infrastructure.