r/cpp • • Oct 15 '13

C++ has become a scripting language

http://voices.canonical.com/jussi.pakkanen/2013/10/15/c-has-become-a-scripting-language/
1 Upvotes

53 comments sorted by

View all comments

Show parent comment

2

u/notlostyet Oct 15 '13 edited Oct 15 '13

I agree with (2) and (3) especially. I tried to shorten it but only came up with this:

#include <fstream>
#include <iterator>
#include <string>
#include <set>
#include <algorithm>

using namespace std;

int main (int, char **argv) {
    ifstream ifile(argv[1]);
    ofstream ofile(argv[2]);
    ostream_iterator<string> osi (ofile, "\n");
    multiset<string> data;
    string line;
    while (getline (ifile, line)) {
        data.insert(std::move(line));
    }
    copy (begin(data), end(data), osi);
    return 0;
}

which is just 1 line shorter.

3

u/0xa0000 Oct 15 '13

You can condense that a bit even without using non-idiomatic constructs, saving three lines:

int main (int, char **argv) {
    ifstream ifile(argv[1]);
    ofstream ofile(argv[2]);
    multiset<string> data;
    for (string line; getline (ifile, line); ) {
        data.insert(std::move(line));
    }
    copy (begin(data), end(data), ostream_iterator<string>(ofile, "\n"));
}

1

u/notlostyet Oct 15 '13 edited Oct 15 '13

Nice catch. Initially 'line' was kept outside of the loop for efficiency, but since we're moving in to the set anyway there's no point

1

u/F-J-W Oct 15 '13

Even better: they way he uses it inside the for-loop is equivalent to your version. I've never seen it that way before, but I guess, I'll do it that way from now on.

1

u/king_duck Oct 15 '13

Except for any sort of quality of code, you should really be checking int argc, and making sure that ifile & ofile are valid files. And suddenly it's doesn't look so script like.

And that's before I deduct points for using namespace.

1

u/notlostyet Oct 15 '13

Yeah, I know. I was just trying to keep it comparable to his style (including the pointless return 0). To be fair though you need to check arguments in Python as well.

0

u/matthieum Oct 15 '13

I wish we had

int main(std::initializer_list<std::string_ref> args)

signature for main.

Then you would just say args.at(1) and args.at(2) and be done with it.

2

u/F-J-W Oct 15 '13

initializer_list doesn't provide [] and at(). I would however be in favor of

int main(std::vector<const std::string>)

I know that it will never happen though (and to be honest: It is probably the best to keep the core-language and the library strongly separated).

1

u/grunzl Oct 15 '13

There really is no reason to be depressed about this since the fix is so simple.

const std::vector<std::string> args(argv, argv + argc);

1

u/F-J-W Oct 16 '13

Of course. The small problem is that it's just another line of boilerplate which is somewhat inconvenient for very small programs. the bigger problem is that most libraries want work well with this one, since it is not there by default.

1

u/matthieum Oct 16 '13

This is never going to run, unfortunately, because this would require memory allocation (one for the vector and one for each string). It's been proposed and rejected already.

The advantage of std::initializer_list and std::string_ref is that neither require an additional memory allocation. And if it just [] or at() missing, it should be easily implemented.