Re:Why C++ sucks (2016 edition)
by loup-vaillant on Monday February 22, 2016 @08:44AM
(#11150707)
Thanks for the tips, I'll apply them. Just a couple questions:
* Do you have a link about the _ (underscore) being forbidden? Our current coding rule make us use them right now.
* Is there a standard exception object I could throw? If I could just write something like `throw std::invalid_argument("what String");` that would be terrific. I don't want to construct a whole class just for that. (Maybe I have to, though?)
I'll also seriously consider boost. While it's a huge dependency, we are on Debian GNU/Linux, so it's probably not that bad.
You're welcome. The underscore thing is mentioned in 17.6.4.3.2 Global names, however it's more complex than what I said initially (this is C++ after all):
* double underscore or underscore + capital letter is reserved in any scope
* underscore is reserved in global scope
Your code is allowed in fact, sorry. My personal rule is that I never start an identifier with an underscore anywhere.
I missed the exc question: yes, invalid_argument is perfectly acceptable in my opinion. However, if you want to inherit from invalid_arg that is also possible - this would allow you to catch only this very specific type of error.
If you stick to your code you should at least:
* add a copy constructor or delete it explicitly
* pass in the vector as unique_ptr to the constructor. This way the "takes ownership" will be clear without the need of a comment.
* add your missing includes e.g. vector
* stop using _ as variable prefix, it's forbidden
* throw from the constructor, don't exit
Boost variant + boost vector will allow you to eliminate all the manual memory management (boost vector allows incomplete types).