19 ms·
#include <print> int main() { std::print("hello, world!\n"); return 0; } So, it has come to this. Much better than overlo
by enriquto 3y ago
#include <print>
int main()
{
std::print("hello, world!\n");
return 0;
}
So, it has come to this. Much better than overloading bit-shift operators for input/output.
- fractalb 3y agoTime to rewrite helloworld programs. IIRC, Java is also making it simpler to a helloworld program.
- thibran 3y agoWhere is the std:: part coming from / why is it not just print(...)?
- goalieca 3y agoHundreds of not thousands of people made their own print over the years. Namespacing is important.
- aodonnell2536 3y agoThe function is part of the std namespace, just like std::cout.
- tialaramex 3y agoC++ has namespacing which makes sense because this language has an enormous amount of available 3rd party libraries and without name spacing you can't help stepping on each others toes. There are two ways you might want to have this work anyway despite namespacing. One option would be that you just import the namespace and get all the stuff in it, this is popular in Java for example, however in C++ this is a bit fraught because while you can import a namespace, you actually get everything from that namespace and all containing namespaces. Because the C++ standard library defines a huge variety of symbols, if you do this you get almost all of those symbols. Most words you might think of are in fact standard library symbols in C++, std::end, std::move, std::array, and so on. So if you imported all these symbols into your namespace it's easy to accidentally trip yourself, thus it's usual not to do so at all. Another option would be to have some magic that introduces certain commonly used features, Rust's preludes do this, both generally (having namespaces for the explicit purpose of exporting names you'd often want together) and specifically ("the" standard library prelude for your Edition is automatically injected into all your Rust software by default). C++ could in principle do something like this but it does not.
- WalterBright 3y agoD avoids the stepping on each other problem by using modules.
- glouwbug 3y agoCan D compile modules in parallel?
- WalterBright 3y agoThe language is designed so that is possible, although the current compiler does not. At one point, the compiler did all the file reading in parallel, but that was finally turned off because it did not significantly improve compile speed.
- dkersten 3y agoThe std namespace is from the <print> standard header. It’s not just print because while you might want it in the global namespace, other people do not. For example, my code isn’t cli and doesn’t need to print to the cli, but perhaps I want to print to a printer or something else and have my own print function. Leave un-namespaced identifiers to those that are declared in the current file and namespace everything else. If you really want, you’re free to add “using namespace std” or otherwise alias the namespace, but keeping standard library functions out of the global namespace as a default is a good thing! (In any language, not just C++)
- JohnFen 3y ago> If you really want, you’re free to add “using namespace std” You're free to, but I discourage the habit. It's more verbose to add the namespace:: prefix to symbols, but it sure does make it easier on the devs that have to work with the code later.
- dkersten 3y agoOh, I’m completely with you on this and always prefix my namespaces. Occasionally I will alias long namespaces to short ones, but I never pull identifiers into the global scope and I really dislike when I see code online that does “using namespace” (unless it’s tightly scoped, at least). I’ve been prefixing std:: for years and won’t stop now, I like knowing where an identifier is coming from, which is extra important when you have multiple types of similar containers that you use for different performance characteristics (eg abseil, folly, immer versions of containers vs std containers)
- turndown 3y agoIs the include correct? Wouldn't it just be import std; ?
- enriquto 3y agoApparently, the header print adds symbols to the std namespace, just like other standard headers: https://en.cppreference.com/w/cpp/header/print https://en.cppreference.com/w/cpp/header/print
- kstrauser 3y agoWhat's the difference between include and import now?
- gpderetta 3y agoModules.
- lostmsu 3y agoAre they in C++23?
- gpderetta 3y agoThey are in C++20. But only MSVC has an usable implementation yet.
- kazinator 3y agoSome C++ person recently wrote to the GNU Make mailing list about some grotesque experiments for supporting C++ modules inside GNU Make whereby GNU Make would communicate with some C++ compiler process over sockets and generate dependencies. Any decent language with modules needs no make utility in the first place. You tell the compiler to build/update the program. The compiler compiles (if necessary) the interface definitions the program references, and in that vein recursively updates the whole tree, compiling anything that has changed; then links the program. I didn't need any make utility when working with Modula-2 in 1990!
- nuancebydefault 3y agoAre there now in c++, after all these years, f-strings like python has, or at least something coming close? If not, I keep being at my disappointed state about c++.
- enriquto 3y agoYou still have cstdio, which is perfectly cromulent. There's also a format-like thing using integers surrounded by curly braces, but it's horrific and I'm afraid to copy-paste it here.
- Longhanks 3y agoYes, already since C++20. But if you really would have wanted to change your mind, you could’ve found out easily.
- LordDragonfang 3y ago>Already since C++20 ...Or phrased another way, they finally added them in the latest spec (which the majority of C++ devs aren't even using yet)
- deleted 3y ago[deleted]
- akhosravian 3y agoC++23 was finalized nearly six months ago so it is not correct to call C++20 the “latest spec”.
- bippingchip 3y agoIf you just need a nice print: fmtlib is a really nice c++23 style implementation without needing c++23 compiler support. Highly recommend it. It’s simple. It’s fast.
- xxpor 3y agoputs on the most annoying code reviewer you've ever met hat acktually, you should use println.
- coldpie 3y agoOperator overloading is an anti-feature and every language with it, would be better without it. Fight me.
- mhh__ 3y agoWhat's wrong with being able to add my Complex or Dual numbers together?
- coldpie 3y agoWhatever you save in avoiding having to write "sum = add(l, r)" is not worth allowing programmers to bitshift file handles[1], divide file path objects[2], or subscript objects to launch a process[3]. [1] https://en.cppreference.com/w/cpp/io/cout https://en.cppreference.com/w/cpp/io/cout [2] https://docs.python.org/3/library/pathlib.html https://docs.python.org/3/library/pathlib.html [3] https://pypi.org/project/plumbum/ https://pypi.org/project/plumbum/
- mhh__ 3y agoC++ "bitshifts" (yes, but it's just a symbol in this context) make no real difference to me. It was type safe when it wasn't easy to be back in the day, everyone knows exactly how they work - it's never shot me in the foot (whereas the real meat of the design has, independently of how the composition is expressed syntactically). The others are more sus but you can make a bad API out of anything. Rewriting (say) a load of calculations as a tree of sum pow exp etc. is just a huge burden - a codebase I work on has a formula that takes up about 6 lines for example, outputted by mathematica: total pain to to translate to function calls.
- coldpie 3y ago> The others are more sus but you can make a bad API out of anything. Yahhhh but there's something about operator overloading that is like catnip to clever programmers. Ah ha, file paths have slashes, and the divide operator is a slash! Clearly I should use the divide operator to append paths segments together! I'm so clever! Ah ha, << looks kinda like an arrow I guess, so we can use it to, uh, pass data between objects, I guess. I'm so clever...? It's irresistible. We have abused it and we must give it up for the good of all.
- mhh__ 3y agoAdmittedly I grew up after their introduction but I've always viewed the whining about overloading the shift operators quite pointless. Of all the things C++ annoys me with, I don't care that much about syntax.
- tomcam 3y agoMy guess is you never had to parachute into a project using operator overloading in strange, inconsistent, and undocumented ways with no original maintainers to show you the ropes
- mhh__ 3y agoAfter all it's impossible to do this with regular code
- amalcon 3y agoI actually like operator overloading, but overloading the shift operators for I/O was still a mistake IMO. It's a mistake even if you ignore that it's a theoretical misuse (I/O and binary shifting have nothing to do with each other semantically). The operator precedence of the binary shift operators is just wrong for I/O.
- JackSlateur 3y agoSo: int main() { printf("hello, world!\n"); return 0; } C knew it all along :)
- Dylan16807 3y agoPrintf is fine until you have to deal with the "f". Some compilers can keep format strings type-safe, but they are going above and beyond the standard to make it happen.
- tristan957 3y agoIf you are using a compiler that doesn't support this, developer tooling like clangd and clang-tidy can help catch these issues too.
- Dylan16807 3y agoToday, yes. But it's relevant for a claim that C had it right "all along".
- LordShredda 3y agoMaybe this should've been something built in if automated tooling can catch it?
- kazinator 3y ago> above and beyond the standard That's a very low bar though. There are widely used languages that don't have a standard at all; literally every single thing you can do in them is above and beyond any sort of standard.
- Dylan16807 3y agoFew of those languages corrupt the stack when you print wrong.
- qalmakka 3y ago
- Google234 3y agoDon’t need the return 0?
- kazinator 3y agoHow long before they get it down to: main() { puts("Hello, world"); }
- tialaramex 3y agoBarry Revzin's comparison to what Rust chose here is interesting https://brevzin.github.io/c++/2023/01/02/rust-cpp-format/ https://brevzin.github.io/c++/2023/01/02/rust-cpp-format/ I think Barry under-estimates how long it will be before C++ programmers actually get the equivalent of #[derive(Debug)] and as a result the impact on ergonomics. But of course I might be wrong.