7 ms·
What operator would you have used?
by eklitzke 5y ago
What operator would you have used?
- colejohnson66 5y agoNone. Just use a simple function call like almost every other language. Why is std::cout::write(myInt) such a hard thing to write that we decided std::cout << myInt was a better idea?
- trinovantes 5y agoIf I have to guess, it's probably to support operator overloading (printing custom data type) without requiring OOP e.g. must be a class that implements Writable
- fbkr 5y agoDisclaimer: I dislike iostreams and in our environment <iostream> is completely banned. However, in C++98, how else would you handle when you have an arbitrary number of things to print in a type-safe way? e.g. cout << myInt << ": " << something << " - " << otherthing << " - " << etc << '\n'; printf is not type-safe, nor supports user defined types. I'd rather not write 8 lines of functions calls for the same thing either. I think it made some sense C++98, though it is an awful API in 2021.
- google234123 5y agostd::cout::write(myInt,": ",something," - ",otherthing," - ",etc,'\n'); Possible with preprocessor.
- kayamon 5y ago> However, in C++98, how else would you handle when you have an arbitrary number of things to print in a type-safe way? e.g. If I were the one designing the language, I'd have simply fixed printf to be type-safe. (e.g. via adding macro support a la Lisp)
- fivea 5y ago> What operator would you have used? Qt does it right: fluent-style interface which does string interpolation.
- astrange 5y agoIs that localizable, or does it compile the interpolated string into code so it can't be replaced?
- slavik81 5y agoQt has a pretty extensive localization framework. You would probably want to wrap both strings in tr(). https://doc.qt.io/qt-5/i18n-source-translation.html https://doc.qt.io/qt-5/i18n-source-translation.html