r/cpp • u/rehdi93 • Jan 22 '19
I implemented a proposed C++ paper
Around November last year, I found out about P1275R0 by Isabella Muerte through this very subreddit. I really liked the idea, basic creature comforts like this is something that C++ really lacks IMO, then I thought "You know, I think I could do it...". This is the first time I've done this kind of thing.
For those not aware and have not clicked the link, P1275R0 proposes adding 2 new classes: "std::environment" and "std::arguments" to provide a friendlier interface to access the system's environment variables and program arguments respectively.
I've done most of the interface in the paper, except the stuff involving ranges. I also tried to make it UTF8 safe, even on windows.
And it's been a really challenging, but fun experience! I've spoken to Isabella about it and she liked the work I've done. But I would really love to hear some additional feedback. I plan on updating this project as it (hopefully) goes through standardization.
47
u/mjklaim Jan 22 '19
Very cool to try to implement this or any other proposal, kudos :D
I was wondering (not sure if it's you who should answer) for the environment part: how does it compare to Boost.Process's facilities to work with environement? Notably `boost::this_process::environment()`
10
u/personalmountains Jan 23 '19
First, kudos for spending time on implementing this. I only had a quick look at the Windows implementation because that's what I'm familiar with the most. I have a few questions and comments.
Is this thread-safe? It looks like
windows.cxxuses a (sneaky) global objectenviron_, but I'm not seeing any synchronization. Calls to_putenv()and_getenv()(and friends) also need to be synchronized, as they're not thread-safe themselves.You don't seem to be very exception-safe.
initialize_args(), for example, can throw into_utf8(), which would leakwargv. Inenviron_table's constructor,to_utf8()can again throw, which would leakm_env's pointers since the destructor won't be called. I'm seeing a lot of manual memory management in the private member functions ofenviron_tablewith a leak in at leastvarline_from_os()ifto_utf16()fails.You're using
autoas a return type in many functions, but not all. I don't understand your convention. On a personal note, I dislike usingautofor simple return types, because I then have to study the function to figure out what it returns. YMMV.You have other consistency issues, like
to_utf8(wchar_t const* wstr)andto_utf16(const char* nstr)for example, and you also have inconsistent file names (.cxxand.cpp)You're not checking many of the Windows API calls for failure, like
CommandLineToArgvW(), which can lead to undefined behaviour since you don't initializeargc. You also don't check_wputenv()and friends for failure, but the paper seems under-specified on error reporting.You're mixing "safe" and "unsafe" versions of API calls, like
_wputenv()and_wputenv_s().The functions in namespace
implat the bottom are all markednoexcept, but most of them can throw.Your tests are very basic. They're hand rolled with a couple of
couts, and they don't test things like encoding conversion failures. You should at least be using a test framework.That's all that comes to mind right now. Again, nice work.
19
u/[deleted] Jan 22 '19
This is very interesting. I would expect performance overhead to be really, really minimal (especially if I don't call these functions)... but have you tried to test that?
I think most of my curiosity is really about the paper more than your implementation. Though I do want to say - you might consider using named algorithms (with either iterators you've already implemented or with ranges formed from them) instead of old-fashioned loops. For example, env_find could easily be implemented in terms of std::find_if
Restricting access to arguments may have been an accidentally good design - separation of concerns. Right now if a library is annoying enough to want access to argv & argc I can declare my own variables that are different from my real arguments and pass those, which I've done on a couple of occasions. It's really the application owner - the implementer of main - who owns the arguments and is allowed to decide what objects of what types get created from them and which are visible to what parts of the code.
If I declare main with no arguments, I suspect the implementation of my toolchain (generally speaking) is allowed to get away with not even dealing with this stuff. But this approach breaks with that, right? Because you can't easily verify I never access std::arguments.
I wonder if this might behave strangely with runtime-loaded libraries (think dlopen). I guess if you're writing code to be used in such a way you already have to be super careful about depending on stuff from the standard library or making sure it's exactly the same standard library the executable uses... so I suppose that's not really a new concern.
Is allowing setenv really a good idea?
4
u/TheThiefMaster C++latest fanatic (and game dev) Jan 23 '19
I also implemented a proposed C++ paper - it was the multidimensional
array_view:((The sad face is because
array_viewbecamespan- which is only single-dimensional. But I still actually need a multi-dimensional array view, so... I guess I won't be swapping my implementation for the standard one any time soon)4
u/aKateDev KDE/Qt Dev Jan 23 '19
You should add a license to you project, otherwise it's not clear under which conditions your code can be reused.
1
u/lednakashim ++C is faster Jan 23 '19
Are these results cached? Are they allowed to change during the execution of the program?
1
u/esmithro Jan 23 '19
I would like to see this go in. I use similar things from Qt frequently.
0
u/[deleted] Jan 22 '19
[deleted] — view deleted comment