0

I've had this question on my C++ quiz.

To use c++ standard library we write ...

  1. #include <iostream>
  2. using namespace std;
  3. import std;
  4. None of the above

I've answered it #include <iostream>. Because indeed <iostream> is a standard C++ library. When I asked the professor about the correct answer he said the correct answer is using namespace std;. Actually, the correct answer doesn't make sense to me. And also the question is confusing.

Mohamed Magdy
  • 345
  • 3
  • 15
  • 7
    That is a pretty bad question. You can use the standard library, and most the time should, without including "using namespace std;" in your program. Also, you could include "using namespace std;" but then never actually use the standard library. Not sure what he is going for if what you have written is the full question. – Keith Johnson Jul 20 '21 at 09:56
  • 4
    The question is poorly formulated. I would have chosen `#include ` or even "none of the above", as nowhere are using-directives mentioned in the [Using the library](https://eel.is/c++draft/using) section of the C++ standard. – L. F. Jul 20 '21 at 09:57
  • 1
    Despite the 'using' keyword, you don't actually need to use 'using namespace std' to actually use std. Just prefix with the namespace and that's it. Including the headers is more logical, but... iostream is only part of the standard lib. Including that header is not enough to use everything from std. One really mandatory thing is to link it, and that's not included in the answers. – Adrian Roman Jul 20 '21 at 09:57
  • 6
    Related: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/9716597) – L. F. Jul 20 '21 at 09:58
  • 3
    "To use c++ standard library we write ..." - to use it you need a C++ compiler and stdlib headers. `using namespace std` isn't a requirement on top of that - hell, as already linked, it's discouraged to use. Horribly phrased question, though - I recommend complaining about it to someone of significance at , because #2 is everything but correct the way the question is worded – Zoe Jul 20 '21 at 10:05
  • `#include ` :-) – Jarod42 Jul 20 '21 at 10:11
  • I guess it's true. Those who can't do... teach. – StoryTeller - Unslander Monica Jul 20 '21 at 10:30
  • 3
    If it's any consolation, `1` is definitely the wrong answer. The correct answer is `4`. The professor's answer of `2` is also definitely the wrong answer. – Eljay Jul 20 '21 at 11:12
  • 2
    @Eljay -- agreed, but the professor's answer reflects a common misconception, perhaps imported from other languages. – Pete Becker Jul 20 '21 at 14:04
  • @Eljay Why 1 is definitely the wrong answer? – Mohamed Magdy Jul 20 '21 at 14:31
  • @user8721005 iostream is a part of the stdlib. Importing it is importing a part of the stdlib, but not the entire stdlib – Zoe Jul 20 '21 at 14:34
  • In this test, you give zero mark to professor. – Sreeraj Chundayil Jul 22 '21 at 10:54

1 Answers1

7

the correct answer is 4, "To use c++ standard library" doesn't really make any sense, you can use individual classes and functions from the standard library not the whole standard library.

  1. This includes one header from the standard library allowing use of std::cin etc.
  2. see Why is "using namespace std;" considered bad practice?, this just imports the std namespace into the global namespace and is not necessary to "use the c++ standard library"
  3. When the standard library supports c++ modules this could be the correct answer
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60