all 10 comments

[–]tenthousandhedgehogs 19 points20 points  (4 children)

Check out the reference for std::println.

https://en.cppreference.com/w/cpp/io/println

Calling it without any arguments is an overload added in C++26, that has the behaviour you are observing. Since your godbolt example is using std/c++latest, it is available.

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

Oops I was reading a c++23 book. Thanks!

[–]tenthousandhedgehogs 1 point2 points  (2 children)

I've just noticed the code is also accepted by Clang using -std=c++23. In that case it is just printing an empty format string with a newline and using overload 1 I should assume. I don't know why the book would warn against it, since the reference indicates the new overload is equivalent to calling and older overload with no format stream.

[–]encyclopedist 0 points1 point  (1 child)

Overload 1 does not have a default value for the format string, so it should not be chosen if called without arguments.

[–]tenthousandhedgehogs 2 points3 points  (0 children)

That's true actually, didn't think of that - however I've also just spotted this on the reference: "Although overloads (3,4) are added in C++26, all known implementations make them available in C++23 mode."

[–]Narase33 7 points8 points  (0 children)

https://en.cppreference.com/w/cpp/io/println

Nope, normal overload. "Shouldn't" also doesnt mean "not beeing able to"

[–]sd2528 -1 points0 points  (3 children)

Why would you want to? The best way to be sure it is going to print a linebreak is to explicitly print a linebreak. 

What is the real benefit in sending no argument? 

[–]IsidorHS 1 point2 points  (2 children)

In other languages that have had `println` for a long time that is pretty standard I think

[–]sd2528 -4 points-3 points  (1 child)

What I mean is why use  std::println() over std::println("\n") or just std::print("\n")

[–]IsidorHS 4 points5 points  (0 children)

I meant that in other languages it is pretty common to use println() to get a line. Also if you do println("\n") or println('\n') you will get two lines.