-1

For eg, if you google "the system() function in c++ tutorialspoint/geeksforgeeks/cppreference", you'd come across tutorials and references of how to use the system() function in your program, along with various parameters you can pass to get various jobs done (for example in the case of system(), we have system("clear") system("date") etc)

But I think this usage must be defined somewhere in one of the header files right? So I can just go there and read it instead of coming online.

Well the issue is that I tried going to the definition of the function system() which was located in stdlib.h but nothing written there makes sense to me. I tried searching where the usage of system("clear") might have been defined using the trusty ctrl+f in the stdlib.h file but guess what(?), nothing came up.

Can someone help me achieve this self reliance I'm trying to build? thanks.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
Typedragon33
  • 139
  • 7
  • 2
    "But I think this usage must be defined somewhere in one of the header files right?" - header files only contain enough information for the compiler and linker to do their job - not yours. – Dai Jan 04 '21 at 01:44
  • 2
    Have you really read [cppreference](https://en.cppreference.com/w/cpp/utility/program/system)? "Calls the host environment's command processor (e.g. /bin/sh, cmd.exe, command.com) " -- i.e. to learn what system("clear") does, you have to learn sh (/bash), not C++. – user202729 Jan 04 '21 at 01:44
  • 3
    If you're trying to learn C++ attempting to use reference material to expand your knowledge of core fundamentals, this is unlikely to be very productive. On the other hand if you already have core C++ fundamentals, experience, and some basic knowledge of some parts of the C++ library, then using the reference material to learn and understand other parts of the C++ library, and expand your ***existing*** knowledge, is certainly viable. But, if you're in that situation you would already know how to forge the path ahead, yourself. If not: get a C++ textbook. – Sam Varshavchik Jan 04 '21 at 01:49
  • 1
    You are misunderstanding self reliance. Self reliance in this day and age doesn't mean "don't use the internet". The internet is so ubiqous that it's an indispensable tool. Self reliance means knowing how to search, how to filter and judge good and bad information, how to read a documentation and how to adapt what you find to your particular needs. There is a reason the saying is "read the *sweet* manual" and not "read the source code". So just use in good conscience references like cpprefernece etc – bolov Jan 04 '21 at 01:50
  • Also, it goes without saying systematic learning from a good resource like a good book should be the main source of learning. Searching the internet and looking at documentation should be complementary. – bolov Jan 04 '21 at 01:53
  • The short answer is, "no." C++ apis, in particular, are very versatile in that they do a very small task well. Good tutorials apply a sane narrative to the use of the API and do not cover all uses. Putting such specialized knowledge in the C++ stdlib would make the documentation enourmous. Use the internet. Use colleagues. These are valid ways to gain the expertise in C++ and the standard library. Reading a book will never eclipse experience (but I always recommend it!) Sorry if that's a non-answer. – Sam Jan 04 '21 at 01:55
  • 1
    I can't speak for other compilers, but the MSVC headers are not documented, so unless you want to read and understand all the code, then you can't really. You could read the standard, but that might get rather confusing, and is not the best to learn from. What is wrong with using cppreference (or similar)? – ChrisMM Jan 04 '21 at 01:55
  • 2
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Remy Lebeau Jan 04 '21 at 01:56
  • Header files are typically intended to only be read by a compiler. The compiler typically only needs enough information so it can check if code uses data structures or functions in a way consistent with their declaration. A human needs to actually write that code so, unless they rely on guesswork, needs to understand what the members of data structure mean, how to set their values, and what functions will achieve. In other words, humans need to understand more than compilers to do their job. A human who only reads header files will therefore be unable to do their job. – Peter Jan 04 '21 at 02:02
  • @Dai Well, a good header file _should_ contain enough documenting comments for every-day use, though the standard library implementations are a fair exemption from that in my mind. – Asteroids With Wings Jan 04 '21 at 02:04
  • @Peter _"Header files are typically intended to only be read by a compiler."_ Standard library ones, yes. Non-standard-library ones that couldn't be further from the truth. – Asteroids With Wings Jan 04 '21 at 02:11
  • @AsteroidsWithWings - The question is about learning the C++ standard library. – Peter Jan 04 '21 at 02:16
  • @Peter I am aware. However, you said _"Header files are typically intended to only be read by a compiler. [..] A human who only reads header files will therefore be unable to do their job."_, and that is false. – Asteroids With Wings Jan 04 '21 at 02:17
  • 1
    Side note about `system`: Use it sparingly. when you call `system` your program stops, another program starts, runs to completion, and exits. That is a lot of overhead. Sometimes it's justified, you've already got a program to do Job X and Job X is so expensive that no one'll see the extra process overhead or the task is a one-and-done so there's no point to turning X into a library. But for stuff like pausing or clearing the console, there'll be a console API call that does the job with much less fuss. Once you know the language and the APIs, then you can be self-reliant. – user4581301 Jan 04 '21 at 03:25

2 Answers2

2

There's really only one way to be completely unreliant on third-party material: purchase a copy of the standard (or download a close-to-publication working draft version!).

But that's not a very good way. The standard is, by design, quite esoteric. It's not designed for everyday reference use. Instead, there are references online for that (e.g. https://www.cppreference.com).

The headers on your system aren't much use either. In a perfect world, all library headers have enough documenting comments for day-to-day use, and many "third-party" libraries do this. But the standard library implementations tend to be considered "internals" and are often even more esoteric to read than the standard text. I've certainly never seen any reference material in them.

If you're concerned about losing your internet connection, you might consider checking out the cppreference.com source (seriously!) and "hosting" it locally on your computer, for personal use. Be sure to update to HEAD once in a while.

(And, for some standard-but-not-C++ features, like POSIX functions, you may find manpages on your system with useful information.)

Otherwise, just bite the bullet and Google when you need to look something up. That's what the rest of us do.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • I'd upvote your answer if I thought the question was answerable and in its current state it's not. – Ted Lyngmo Jan 04 '21 at 02:18
  • @Ted Okay, but it is, and the fact I've literally answered it proves that... – Asteroids With Wings Jan 04 '21 at 02:18
  • :-) I'm pretty sure that's not "proof". No worry. I do like what you write and had a pretty long answer to the question myself and then I wondered - WHY? The question is misplaced. – Ted Lyngmo Jan 04 '21 at 02:22
  • 1
    The standard is also written for use by implementers (e.g. of compilers and their associated standard library) to assess correctness of their implementation. There is a significant level of knowledge of programming and computer science assumed. It is not written as a teaching aid. – Peter Jan 04 '21 at 02:23
  • @Peter Indeed. As I say, it's esoteric by design. – Asteroids With Wings Jan 04 '21 at 02:35
0

Yes, and while the stdlib.h does not have such comments, more recent, c++ specific headers, at least for MSVC, do include usage comments. Here is a bit from the namespace filesystem in < filesystem > using MSVC's header.

   inline bool copy_file(const path& _From, const path& _To) {
        // copy a file _From -> _To, failing if the destination exists
        return _STD filesystem::copy_file(_From, _To, copy_options::none);
    }

Similar comments appear in the Path class for Path specific methods.

These are browsable with an IDE such as VSCode.

Great question. Inspires me to look here first. Always best to get your information from "the source" neh! But this only partially answers your question. Hopefully, someone has a suggestion on how to get at such usage comments for the cstdlib or maybe some older libraries where they are not immediately seen in the header!