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

16

u/noname-_- Oct 15 '13

So I actually came up with a way of sort of using gcc for actual (almost) scripting a while back, just for fun. As in making the source file executable in a unix like environment. I was experimenting with C but it's equally applicable to C++. Here's what I came up with...

//`which true`; set -e; name=`mktemp -t gXXXXXX`; lf=`pkg-config --libs --cflags sdl`; gcc -xc -pipe -std=c99 $0 $lf -o $name; exec -a $0 $name ${*:1}; ret=$?; rm $name; exit $ret

#include <stdio.h>

int main(int argc, char** argv)
{
        for(int i = 0; i < argc; i++)
                printf("%s\n", argv[i]);
        return 32;
}

If you paste that into a file and make it executable, you can execute it and it will run the C program with correct argv (including argv[0]) and return code.

Breakdown of the "magic":

First of all, the reason this hack works at all is because how Unix shells work. With an executable file, in the absence of a hashbang, a shell will first try and run the file as a native binary. If that fails it will assume that the program is a shell script, for legacy reasons. So in the first "iteration" the file will be executed as a shell script.

//`which true`; 

Does absolutely nothing. On Linux it will execute ///bin/true, which will immediately terminate successfully. The reason it uses "which" is for compatibility between OSX and Linux, and the reason it's even there is because it conveniently makes the first line a comment in C99 and C++.

set -e;

Fail on errors, eg. if a library isn't found or if there's some syntax error in the C code so gcc fails the error will be reported and execution will stop.

name=`mktemp -t gXXXXXX`;

Create a temporary location to store an executable. The -t stuff is a required template on OSX, but isn't required with GNU.

lf=`pkg-config --libs --cflags sdl`;

Use pkg-config to generate compiler and link flags for any libraries you use. I just put SDL in there as an example, the code doesn't really require it.

The reason it's on its own "line" and not inlined in the gcc call is because I want the script to fail gracefully when a library isn't found, instead of trying to compile without it.

gcc -xc -pipe -std=c99 $0 $lf -o $name;

-xc specifies that the file is C, so that if the file isn't named .c (which it probably isn't) it's still compiled as C.

-pipe just speeds up compilation a bit by passing stuff around between the different gcc programs in a pipe instead of via temp files.

-std=c99 pretty self explanatory.

$0 compile whatever file used to make this call, ie. this file.

$if aforementioned linker and compilation flags.

-o $name output to the generated temp file so that it doesn't conflict with any other instances of the program being run.

Alright, now we have a binary (hopefully)!

exec -a $0 $name ${*:1};

Execute the binary with exec. -a is the magic here, because it specifies that this file ($0) is argv[0] for the binary, instead of /tmp/g1203981 or whatever the binary is really called. $name is the binary and ${*:1} passes the rest of the arguments excluding $0.

ret=$?;

After execution, save the return value.

rm $name;

Cleanup, remove the temporary binary.

exit $ret

Terminate with whatever exit code the C program reported. It also conveniently stops the execution of the "shell script". Which is a good thing, since it's actually a C program after this point :)

This was mostly just for fun, and it should be noted that if you actually want to use C (and only C) for scripting, there's a really, really fast compiler called TinyCC that actually supports a proper hashbang for executing C programs directly (#!/usr/bin/tcc -run). The only problem with that approach is that it doesn't support using pkg-config for libraries. And of course that it isn't nearly as ubiquitous on Linux/Unix installations as gcc is.

6

u/Wriiight Oct 16 '13

I strongly suspect gcc would be among the slowest interpreters of any scripting language. But the Ruby folks would finally be able to claim to be faster than C!

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 (0)

0

u/notlostyet Oct 15 '13

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.

None of which are available in GNU stdlibc++ yet :(

3

u/greyfade Oct 15 '13

... I've been using them.

1

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

codecvt_utf8_utf16, codecvt<char16_t, char, ...> etc aren't available afaict. Only the u16string and u32string typedefs and wchar_t based stuff seems to work (wchar_t is implementation defined, 32bit on Linux though).

In any case, writing something like the articles file copy code while doing UTF8 -> UTF16/32 conversion in difficult and unintuitive. It should be easy as imbuing the streams.

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

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.

19

u/snarkhunter Oct 15 '13

They've apparently confused "scripting language" and "high-level language".

6

u/Cosmologicon Oct 15 '13

I agree, but I would argue that "scripting language" is a pretty useless designation these days. 25 years ago when you were choosing between awk and C, the distinction was clear and important, but now we've got things like python and we can't even agree on what's a scripting language anymore, and the thing is, it doesn't really matter.

-8

u/kalcytriol Oct 15 '13

No shift Sherlock. Who is upvoting you for this?

10

u/bames53 Oct 15 '13

With the release of C++11 something quite extraordinary has happened. Its focus on usable libraries, value types and other niceties has turned C++, conceptually, into a scripting language.

The supporting example offered differs from C++98 code only in that it uses a range-for loop and auto for the last little bit. The rest of it is the same as what could have been written any time in the last 15 years.

It's not really C++11 that has enabled this; It's simply that over time 'idiomatic C++' has changed.

Another thing he mentions as being scripting-language-like is a large library. C++11 didn't really add much to the library. Aside from small tweaks all over the place and minor additions like array and tuple, only threading, atomics, and regexs were added in C++11. Far more still lies ahead; ranges, filesystems, networking, high-level task parallelism, graphics, etc.

On the core language side modules ought to help a lot at making C++ more like a scripting language.

1

u/matthieum Oct 15 '13

I agree that without support for filesystem manipulations (and I don't mean creating files: I mean listing directories!) it is hard to consider C++ a scripting language.

4

u/AgentME Oct 15 '13

To me, scripting language means

  • low boilerplate

  • no compiling, no makefiles, no linking headaches

The having a good standard library part is just something that compiled languages have been sorely overdue on.

3

u/green_transistor Oct 16 '13 edited Oct 17 '13

Well anything simple would be small, at least initially. But there's already an incurred complexity when you decided to use C++.

Let me know when you can "script" a web page, like in Python, Ruby or Node.js.

EDIT: Some googling led me to Wt, TreeFrog and qdjango. Interesting stuff.

1

u/by_moon_alone Oct 17 '13

You can in fact create web-pages with C++, if you're into that sort of stuff I mean.

1

u/green_transistor Oct 17 '13 edited Oct 17 '13

Well that would be neat, but a little dangerous too. Is there some C++ project for creating web sites?

1

u/redpoet Oct 17 '13

Upvote for Wt. Wt rocks.

4

u/F-J-W Oct 15 '13

I really like C++ and I start using it almost directly after bash-scripts, but no, C++ is no scripting-language: The boilerplate it needs is pretty small (compared to Java and alike), but it is still to much for scripting:

  1. main-function: Great for any big or intermediate program, but way to much for a three-line-script

  2. includes: Almost everything high-level in C++ needs to be included. Scripting-languages usually provide strings and IO without asking for it. -> Again to much overhead

  3. braces. Don't get me wrong, I like them. But not for scripting. In that area indentation is clearly superior.

Conclusion: All these points could be fixed in a hypothetical dialect that could be trivially translated to C++, but C++ itself is still to heavy.

OTOH: The reasons above are part of the reasons, why I think it is a bad idea to implement big projects in scripting-languages.

2

u/IWantUsToMerge Oct 15 '13

By the way, there is actually a C++ REPL

0

u/kalcytriol Oct 15 '13

C++ as a scripting language? In what universe? I mean: how much weed do you need to start writing stuff like that?

-5

u/pohatu Oct 15 '13 edited Oct 15 '13

hahaha. Scripting languages are for getting stuff done simply and quickly. They get out of your way and let you do things. They feature dynamic types so you don't have to convert ints to chars to strings to floats to sum a value and print it out. They feature REPL style environments so you can code, think and observe output all at the same time.

C++ does pretty much the opposite of all of that. Further, because there are so many damn sub-languages under the c++ umbrella, you can't even sit down and start coding without first thinking, hmm. Do I want to use template metaprogramming, or OOP, or functional styles, or old-school c. By the time you decide you could have already been done scripting your fizzbuzz with a real scripting language. (though to be fair other languages pose the same challenges (python/js)).

case in point: http://rosettacode.org/wiki/FizzBuzz#C.2B.2B

2

u/[deleted] Oct 15 '13

Woah! The templates version of the C++ code just blew my mind.

-3

u/king_duck Oct 15 '13

Sort of but no. The sort of "Top level" code in you're program can certainly be very simple if you compose your programs out of well rounded modern libraries (including ones you written yourself to do the function of the program)

That said, the code in the libraries themselves is any think but script like. Even the most modern C++ dealing with memory:

 auto p=std::make_unique<my_type>(....);

is hardly script like.

3

u/F-J-W Oct 15 '13

This is neither the most modern way of dealing with memory, nor usually recommended. The correct way is 99% of the time:

my_type p{…};

which is somewhat script-like.

Aside from that: The complexity of the implementation is never really script-like: You probably don't wont to look into the implementation of python tuples and maps.

1

u/king_duck Oct 16 '13

Way to miss the point. Yes at the top level you're right

 my_type p { x, y, z };

is quite clean, but there is practically no way of avoid dynamic memory and resource allocation in any sort of non-trivial inner gubbins of a program.

1

u/F-J-W Oct 16 '13

As I said, it works in 99% of the time, if you do things right. Let's say 95% but if your program is designed well, it is indeed an extremely rare thing.

-4

u/code-master Oct 15 '13 edited Oct 15 '13

The same in Rebol:

REBOL [] files: map-each a system/options/args [to-file a] write/lines files/2 sort read/lines files/1

or:

REBOL [] args: system/options/args in: to-file args/1 out: to-file args/2 write/lines out sort read/lines in

What are you trying to sell?