5 ms·
I find it really problematic that the "classic first program" in this book includes "import std;" as the very first line, and as far as I know not a single comp
by xdavidliu 2y ago
I find it really problematic that the "classic first program" in this book includes "import std;" as the very first line, and as far as I know not a single compiler with the possible exception of MSVC supports that out of the box.
Writing this on a debian machine, and trying "g++ --std=c++23 -fmodules-ts" does not work, and from https://en.cppreference.com/w/cpp/23 https://en.cppreference.com/w/cpp/23 looks like the "paper" for this is P2465R3, for which clang++ 17 has "partial support". I apt installed clang++17, and it still didn't work, complaining "module 'std' not found"
I understand that "import std;" is a very new feature and not "finalized" or whatever, but this book is supposed to be for beginners to C++; I wonder how the average beginner would react to that?
(I found the same thing a year or two ago when reading "Tour of C++")
- davidthewatson 2y agoGood point! Me too. My g++ emits exactly the errors and instructions you described. I haven't read the current Stroustrup version under discussion, but I did read the earlier version when C++ was all day, every day for me. I prefer Koenig and Moo's Accelerated C++ twenty years on though I admit there may be better books since which I've not read. IIRC, their hello world was: // a small C++ program #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; } Which copy pastas into my current arch linux, builds in: g++ a.cpp and runs in: ./a.out Hello World! as it has since I first read it twenty years ago, and likely did before that. The rest of the book was a tour de force of making modern C++ emulate the near-pseudocode, sans pointer arithmetic of C, simple-and-clean, buttoned-up style seen in early python, where none of those things existed until pandas brought wrapped C++ libs along with it like numpy and scipy. Professionally, I've not seen clean C++ code in the wild, though I don't work at FAANG and code reviews are unusual in rapid prototypes so I'm not sure how widely Koenig and Moo are mentored outside the greenfield world I've lived in with the possible exception of radiotherapy where you're trying to avoid being the next Therac 25.
- humanrebar 2y agoAt this point, it's not realistic to maintain interesting C++ programs without a build system. That being said, I expect the specific g++ example you provide will end up working with some special casing in the compiler, at least eventually, when someone submits patches to GCC to get it to work. But mostly, it's not realistic to directly wire up calls to g++ anymore outside of research examples. Note that other compiled languages use systems like gobuild, cargo, Maven, etc. instead of fiddling with gccgo, rustc, javac, etc. directly. https://www.kitware.com/import-std-in-cmake-3-30/ https://www.kitware.com/import-std-in-cmake-3-30/
- billforsternz 2y agoI think your point is orthogonal to OP's point. I agree with him, the first chapter of a book about a programming language should include hello world and it should ruthlessly minimise barriers to the broadest range of possible readers being able to try that out. They can then supplement their reading with active experimentation, a vital step. You can't learn to swim by reading a book about swimming.
- randomname93857 2y agowould this help with your problem with modules, it's referenced on the book's page: https://www.stroustrup.com/module_use.html https://www.stroustrup.com/module_use.html ?
- xdavidliu 2y ago> GCC > CGG (sic) uses .cxx for module files Use -fmodules to use modules. no it does not. Trying this on Fedora 39 for example, my original comment above already had -fmodules-ts, and when I use -fmodules, g++ errors with g++: error: unrecognized command-line option ‘-fmodule’; did you mean ‘-Mmodules’?
- adrian_b 2y agoThe current gcc documentation (13.2.1) says: "_Standard Library Header Units_ The Standard Library is not provided as importable header units. If you want to import such units, you must explicitly build them first. If you do not do this with care, you may have multiple declarations, which the module machinery must merge--compiler resource usage can be affected by how you partition header files into header units." So for now any "import" can work only if one writes appropriate module files first. Nevertheless, I do not expect that the updating of the standard C++ library of gcc will take a long time, so later this new book will become directly usable as it is. For someone who already knows older C++ and just wants to update their knowledge, writing the corresponding module files can be a good learning experience in itself.