January 2007 Archives

2007-01-22

Bjarne Stroustrup's C++ Style and Technique FAQ

I recommend Bjarne Stroustrup's C++ Style and Technique FAQ. This really covers a lot of frequently asked questions.

Posted by Alexander Bernauer | Permanent Link | Categories: C++

2007-01-21

Geeks und der Rest der Welt

Eine Sendung bei /dev/radio über Geeks, das Bürgernetz und den CCC Ulm.

Posted by Alexander Bernauer | Permanent Link | Categories: /dev/radio

2007-01-10

ein häufiger Fehler

Korrelation bedeutet nicht Kausalität.

Posted by Alexander Bernauer | Permanent Link | Categories: Zitate

2007-01-10

iostream trap

Look at this program

#include <iostream>
#include <string>
using namespace std;

int main() {
    cout << "intput 1" << endl;
    int i;
    cin >> i;

    cout << "input 2" << endl;
    string foo;
    getline(cin, foo);
    return 0;
}

A sample run of this program is:

input 1
1
input 2
It feels like the getline command is not executed. If you print the foo string afterwards it is empty. What is going on?

The reason for this is that cin >> i reads an integer from the input stream. This integer is delimited by the newline which gets into the input stream as the user hits return - and stays there. The subsequent getline then reads from the input stream until the first newline which in this case is the one before. So getline does nothing but consuming the newline.

One can fix this situation by calling getline twice. A better possibility is to tell the istream library to discard everything until the next newline. This can be done like this:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

I will not start to bash C++ iostreams now. I think just the facts is enough here. Thanks to Roker and netlest on #c++


Posted by Alexander Bernauer | Permanent Link | Categories: C++

2007-01-07

23C3 Nachlese

Eine Sendung bei /dev/radio über den 23C3.

Posted by Alexander Bernauer | Permanent Link | Categories: /dev/radio

2007-01-07

Friedrich Nietzsche

Ich hab jetzt mal beschlossen, Zitate, die mir sehr gut gefallen, zu bloggen, damit ich sie nicht immer vergesse. Mein erstes ist
"Viele sind hartnäckig in bezug auf den einmal eingeschlagnen Weg, wenige in bezug auf das Ziel."
Friedrich Nietzsche

Posted by Alexander Bernauer | Permanent Link | Categories: Zitate

2007-01-07

Spaß mit der deutschen Bahn

Als ich mit heute mal wieder über die deutsche Bahn ärgern durfte, hab ich mir gedacht, dass das alles mal geblogt werden muss. Aber praktischerweise gibt es das schon. www.bahn-spass.de. Da mail ich jetzt mal meine Geschichten hin.

Posted by Alexander Bernauer | Permanent Link | Categories: sonstiges

2007-01-04

C++ Misunderstandings

The gcc documentation has an extra section called "C++ Misunderstandings" telling the people who is to be blamed for the most common annoyances and surprises :-)

The items are

  • static member declarations are not definitions
  • name lookup, templates and accessing members of base classes
  • temporaries may vanish before you expect
  • copy assignment operators copy virtual bases twice
Go read it. I guess every C++ programer will once stumble over each of them.


Posted by Alexander Bernauer | Permanent Link | Categories: C++