Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31645 > unrolled thread

Re: What is your favorite WC++IETJ?

Started byJoshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
First post2015-05-03 22:32 -0500
Last post2015-05-03 22:32 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: What is your favorite WC++IETJ? Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2015-05-03 22:32 -0500

#31645 — Re: What is your favorite WC++IETJ?

FromJoshua Cranmer 🐧 <Pidgeot18@verizon.invalid>
Date2015-05-03 22:32 -0500
SubjectRe: What is your favorite WC++IETJ?
Message-ID<mi6p6o$icf$1@dont-email.me>
On 4/30/2015 6:44 AM, Stefan Ram wrote:
>    C++ can transfer the RHS type to the left side
>    (using »auto«) as a beginner would expect:

This is only true as of C++11, and this particular feature has only been 
reliably implemented cross-platform for a few years...

> final java.util.Set< java.lang.Character >result
> = new java.util.HashSet< java.lang.Character >();

... but Java 7 allowed you to say new HashSet<>(); here.

Also, to be fair, in practice, Java names are almost never used 
fully-qualified. C++ namespaces are more varied in their practical use 
of fully-qualified names, but explicitly using std::foo is fairly common 
in standard style guides (using namespace std; or using std::foo; are 
also common).

>    C++ can manage resources via RAII:
>
> { file f( "tmp.txt" ); ... }
>
>    when the block is exited, even by an exception, the file
>    will be closed automatically. Beginners that already know
>    how to declare a variable do not have to learn anything new.
>
>    Java is difficult: One needs to remember a special "try"
>    syntax that only works for some classes that implement a
>    special interface to come anywhere near RAII, or otherwise,
>    one has to manage the resource manually. Java has several
>    different means to manage resources, C++ nearly always uses
>    RAII uniformly.

The try-resource block is a relatively recent introduction, but it is 
used in all I/O resources in the standard library already. try-finally 
usually serves the same purpose as RAII, with the advantage of letting 
method names be descriptive. Where try-finally falls down is that both 
creation and deletion of most I/O resources can end up throwing, so 
handling multiple simultaneous constructions is problematic.

>    C++ iterates the letters of a string in a way that
>    is easy to read and write for beginners:
>
> #include <iostream>
> #include <ostream>
> #include <string>
> using namespace ::std::literals;
> int main()
> { for( auto c : "abc"s )::std::cout << c << '\n'; }

If I change "abc" to "é", I get
?
?
as output. So it does something which is completely wrong.

Of course, to be fair, it should be noted that since Java is

>    In C++, "\n" will be automatically converted to the line separator
>    of the platform when it is written to a text file.

Only if the file is opened in text mode, which is the default. Which 
means you usually want binary mode, since otherwise writing binary data 
(which is not distinguished from character data at any type level in 
C++) will silently corrupt your file sometimes.

>    Beginners in Java have to learn painfully that "\n" is not
>    necessarily correct sometimes, instead they are supposed to write
>    »java.lang.System.lineSeparator()« sometimes, Wow!

IMHO, just use LF everywhere (unless you're using a protocol that 
requires CRLF). Automatic line conversion tends to be a headache, 
especially when "binary strings" and "character strings" are poorly 
differentiated. And the only major text editor on any system which 
refuses to consider a plain LF as a line terminator is Notepad on 
Windows. So you only need to use CRLF as a line terminator when you want 
to support people opening up your file with Notepad, which is probably a 
very rare occurrence.


Oh, and while we're on the topic of "what's default isn't necessarily 
what you want", it should be noted that you usually want to use for 
(auto &elem : thing), not for (auto elem : thing).

>    What are your favorite WC++IETJs?

Multiple return values via out-parameters. std::pair.

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web