3 ms·
That style of C programming makes me immediately think how it would be neater in C++. Eg the frontpage example: https://gist.github.com/3969351 https://gist.git
by zokier 14y ago
That style of C programming makes me immediately think how it would be neater in C++. Eg the frontpage example: https://gist.github.com/3969351 https://gist.github.com/3969351
Key differences:
* actual namespaces, no need to prefix everything with "JSON_"
* RAII-style resource management, no need for "json_value_free" etc
* objects, so instead of get_array(root_value) you have root_value.get_array()
- huhtenberg 14y ago> json::Array commits = root_value.get_array(); You probably meant: json::Array & commits = root_value.get_array(); Don't really want to copy an array there, right? After that you'll probably be tempted to use std::vector as a basis for json::array. Then you'll realize what an abysmal PoS default vector allocator is, and so you'd want to allow customizing it. Hello templates, and then before you know it, the whole thing not only looks like one of them incomprehensible boost beauties, but also compiles into a meg of code with a dozen dependencies. Neat :)
- zokier 14y ago> After that you'll probably be tempted to use std::vector as a basis for json::array. Then you'll realize what an abysmal PoS default vector allocator is, and so you'd want to allow customizing it. Hello templates, and then before you know it, the whole thing not only looks like one of them incomprehensible boost beauties, but also compiles into a meg of code with a dozen dependencies. Neat :) Writing good code means having discipline to resist all sorts of temptations, such as the one you describe. And not all slippery slopes are actually all that slippery. The one good thing about C++ is that you can pick the features you want and mostly ignore the rest. The reason C++ is so humungous is that everyone is using different subset of it.
- huhtenberg 14y ago> The reason C++ is so humungous is that everyone is using different subset of it. I completely agree with this. Incidentally, this is exactly the reason why C++ libraries are far less useful than the C ones.
- malkia 14y agoRAII would break you if longjmp/setjmp are used. For example if you had the code in C++, but with "C" externs (like zeromq for example), then using RAII might bite someone unexpected where longjmp/setjmp would not unwind and call the destructors.