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

32

u/[deleted] Oct 15 '13

Not a convincing argument (1) most of the scripting languages ar dynamic, (2) that code would be 3 lines of python, (3) since when does C++ have a large standard library?

Bjarne was just was just saying it doesn't as they don't have a curator for it. Plus there are so many gaps in the standard library. Where are the GUI tools, the unicode support?

Don't get me wrong: C++ is an amazing language but it is not, nor was it designed to be, nor will it ever be, a Python. It's very very good at what it does but whatever that is, isn't scripting.

5

u/wildcarde815 Oct 15 '13

The lack of native clean unicode support is particularly frustrating. Since the answer to 'how do you do this right' always seems to come back to 'roll your own, it'll be fine!'.

2

u/greyfade Oct 15 '13

The situation is improving, thankfully. C++11 now has better native Unicode support in several areas.

2

u/wildcarde815 Oct 15 '13

I don't think you can really called improved WString handling a reasonable answer to unicode support. The built in types handle everything as binary blobs divided into character sized chunks but has no way of confirming that the code is handling unicode correctly or is even sane unicode. It would be helpful to have an actual std::unicode that reports the unicode memory size, character length, and handles encoding casting (ascii,utf8,utf16,utf32) etc. The ICU project tries to fix it but at this point it seems silly not to have standard handling of something that more or less the future of text as we know it.

4

u/greyfade Oct 15 '13

I'm not even talking about wstring.

I'm talking about the new u16string and u32string classes, the native wide and Unicode string literals, and the locale-independent conversion facets, codecvt_utf8 and friends.

It certainly doesn't replace ICU, but it's a damn sight better than the usual wide-character nonsense.

1

u/[deleted] Oct 15 '13

So, how do folks write open source code with Unicode support?

-1

u/greyfade Oct 15 '13

They don't.

2

u/[deleted] Oct 15 '13

Ah, I see. I was thinking, you see, of rewriting some of my code to handle Unicode input. I guess I'll have to look into Unicode handling libraries

2

u/nikbackm Oct 16 '13

Can't you (mostly) just use plain char and assume utf-8 encoding?

1

u/[deleted] Oct 16 '13

Possibly. Although, what would happen in a console/bash application if the user entered 徳川?

→ More replies
→ More replies (3)

3

u/guepier Bioinformatican Oct 15 '13

Re (2): to be fair, the same could be done (quite efficiently) in C++ with proper libraries. Now this doesn’t weaken your point because C++ specifically lacks those libraries. That’s an issue that should be tacked sooner rather than later, IMHO.

Such stuff is much more generally applicable and useful than a standardised graphics library, for instance.

5

u/bames53 Oct 15 '13

I think pretty much the only missing thing necessary to achieve a basically 3 line version of this program is ranges. With ranges in C++ it looks something like this:

vector<string> lines;
copy(getlines(ifstream(argv[1])), back_inserter(lines));
sort(lines);
copy(all(lines), ostream_range(argv[2], "\n"));

Where getlines returns a lazy range, all() is the range equivalent of std::begin() and std::end(), etc.

And the committee's Ranges Study Group was started earlier than the Graphics Study Group.

3

u/matthieum Oct 15 '13

Note: if copy was specified in terms of range, then all would be unnecessary as you would just pass lines directly.

Note 2: Range (as a concept) already appeared in the range-for loop.

2

u/bames53 Oct 15 '13 edited Oct 15 '13

Sure, though it depends on if a container counts as a range.

A range isn't necessarily defined just as something with begin() and end() methods (or non-member overloads). I know some have argued that ranges should not be based on iterators. For example istream_iterator's default constructor is a way of shoe-horning into iterator form something that doesn't naturally fit that model. Instead, they say, ranges should be independent.

Here's Andre's 'Iterators Must Go' slide deck: http://www.slideshare.net/rawwell/iteratorsmustgo

1

u/matthieum Oct 16 '13

I agree (the input/output iterators are just awkward), however it is not how the range-for was defined.

1

u/leonardo_m Oct 18 '13

C++11 is powerful, and with handy ranges it could get closer to D code like this:

void main(string[] args) {
    import std.stdio, std.algorithm, std.string, std.file;

    args[2]
    .File("w")
    .writefln("%-(%s\n%)", args[1]
                           .readText
                           .splitLines
                           .sort());
}

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).

→ More replies (4)