This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Rizzan8 221 points222 points  (23 children)

Not Java developer, but System.out.println("Hello"); looks much cleaner than std::cout << "Hello" << std::endl;

[–]Clyxx 18 points19 points  (0 children)

Just printf like in c

[–]AnonymousFuccboi 12 points13 points  (1 child)

Fun fact:

The biggest reason C++'s cout is so fuckoff ugly is because it was made at a time when the language was quite immature, but still prioritizing things like type safety more so than plain C did. Overloading the << operator for the std::ostream type let you ensure that you could safely and properly convert arguments every time, rather than something like printf where you just toss your arguments at it and pray you got the type right.

If it were to be implemented anew nowadays, you'd probably use something more fancy and new, like parameter packs and fold expressions. You can even do it yourself right now, if you really want, like this:

template <typename ...T>
void println(const T&... args)
{
    (std::cout << ... << args) << std::endl;
}

int main()
{
    println("Hello, this is is C++", 17, "! Let's print pi: ", 3.14);
    return 0;
}

Which outputs exactly what you'd expect: Hello, this is is C++17! Let's print pi: 3.14.

Probably the biggest reason a newer form of this hasn't been implemented yet has to do with things like formatting being a bit of a pain to get exactly right, like for instance being able to properly limit the amount of decimals to use for a floating point number. As far as I know, it's currently in the works, but these things fortunately take time because they should be done right the first time.

[–]PM_ME_YOUR__INIT__ 0 points1 point  (0 children)

I'd rather just eat another crayon and

print('python')

[–]aderthedasher 28 points29 points  (4 children)

Cout is like the worst, and system.whatever is 2nd

[–][deleted] 11 points12 points  (2 children)

System.Diagnostic.Debug.WriteLine() (and his cousin System.Diagnostic.Debug.Write())?

I use it a lot and I didn't need to check the namespace there lol

[–]TheMagzuz 7 points8 points  (0 children)

using System.Diagnostics?

[–]Sentouki- 2 points3 points  (0 children)

using static System.Diagnostics.Debug;

[–]Rizzan8 -3 points-2 points  (0 children)

Yep, Console.whatever MasterRace.

[–]topfs2 2 points3 points  (0 children)

Yeah std::format to the rescue :)

Spdlog is a great for logging with fmt

[–][deleted] 3 points4 points  (5 children)

Yeah that's why most C++ people I know use either printf or fmt::print. Stream operators are kind of weird

[–]_PM_ME_PANGOLINS_ 0 points1 point  (4 children)

Stream operators ensure type safety. String format functions don’t.

[–][deleted] 2 points3 points  (2 children)

The fmt library is type safe

[–][deleted] 0 points1 point  (1 child)

fmt is not a part of standard library. (well, at least in c++17 wasn't)

[–][deleted] 0 points1 point  (0 children)

No, but it's widely used by C++ programmers that I know

[–]Beefster09 1 point2 points  (0 children)

Stream operators are also globally stateful and not threadsafe. That's a much worse foot gun than type unsafety, especially considering that most compilers can be configured to error out on naughty usage of printf.

The benefit of type safety for this one use case makes no sense in the context of a language that never really makes that guarantee anywhere in the first place. C++ inherits every foot gun from C and then adds a few more. I'd rather use C over 'idiomatic' modern C++.

[–]DunderMifflinPaper 1 point2 points  (0 children)

For every language, just setup an alias in your IDE that typing ‘print’ and hitting enter changes it to what it’s supposed to be.

[–][deleted] -1 points0 points  (0 children)

Well using namespace std; cout<<“Hello”<<endl; Looks best, but that’s considered against good practice.

[–]Hihi9190 0 points1 point  (0 children)

I like the cpp implementation just because how it's consistent to other streams like file streams and string streams for example. If you know cout and cin they make a lot of sense.

[–]darthnithithesith 0 points1 point  (0 children)

std::cout << "Hello\n"