News for hackers. Stuff that matters.
Re:When noexcept? by lmm on Thursday December 15, 2016 @10:27AM (#13185172)
Do you do RAII? It effectively forces exceptions, because nontrivial initialization necessarily means the possibility of failure, but exceptions are the only mechanism available for constructors to report failure (well, except for even worse things like an errno-like global variable).
Re:When noexcept? by cmrdporcupine on Thursday December 15, 2016 @10:41AM (#13185288)
I've gotten in the habit of doing "RAII"-like as:

  - basic non-throwing initialization only constructor
  - a "bool Initialize" method which does what the constructor would have done, but returns false on fail
  - destructor as usual to clean up
But this is because I'm at Google where exceptions-are-no.
Re:When noexcept? by brandmeyer on Thursday December 15, 2016 @12:28PM (#13186315)
GOOG is a big company, and not everyone handles this the same way. At least some teams are doing what the sibling mentioned: A static factory-like method that returns a discriminated union of either T or an error type. It is conceptually similar to std::variant<T, std::error_condition>, but syntactically optimized for this use case.
Re:When noexcept? by praptak on Thursday December 15, 2016 @01:34PM (#13186889)
Re:When noexcept? by brandmeyer on Thursday December 15, 2016 @02:24PM (#13187374)
Yep, that's the one.
Re:When noexcept? by user5994461 on Thursday December 15, 2016 @05:32PM (#13189238)
I tried to imagine the error message given by this template when something is written wrong. Just had a vision of horror.
Re:When noexcept? by realharo on Thursday December 15, 2016 @10:56AM (#13185409)
So how do you protect yourself against inadvertently passing around uninitialized objects?
Re:When noexcept? by gpderetta on Thursday December 15, 2016 @04:53PM (#13188815)
Exactly.

Objects with an artificial invalid state are an antipattern in idiomatic c++.

Re:When noexcept? by cmrdporcupine on Thursday December 15, 2016 @11:22AM (#13185674)
Cautioun.

And the destructor has to check for initialization before cleanup.

Re:When noexcept? by ryandrake on Thursday December 15, 2016 @02:48PM (#13187606)
Am I the only one that finds it ironic that you have a typo in the word "Caution"?

I'd be very suspicious of any failure avoidance system that relies on "user caution".

Re:When noexcept? by AnimalMuppet on Thursday December 15, 2016 @04:50PM (#13188791)
But all failure avoidance systems rely on user caution. Error return values have to be checked. Exceptions have to be caught and handled (correctly). RAII has to be done everywhere a resource is allocated.

Now, true, some of these impose lesser burdens than others. Arguably, RAII imposes the least - but not zero. And if you forget with any resource, for that resource you are vulnerable.

Re:When noexcept? by lmm on Friday December 16, 2016 @04:37AM (#13192139)
That we can't make things perfect is no reason not to make things better.

And ultimately it should be possible to enforce that all resources are managed, because ultimately all resource interfaces have to be provided by the language runtime. To a certain extent enforcing user-defined invariants is always a matter of taste, but at the same time a language that makes it easy makes people a lot better at it.

Re:When noexcept? by SamReidHughes on Thursday December 15, 2016 @03:33PM (#13188083)
You'll get a run time error.
Re:When noexcept? by lmm on Friday December 16, 2016 @11:50AM (#13194576)
Maybe. Or maybe you'll get an invisible resource leak, or silent data corruption, or an RCE vulnerability.
Re:When noexcept? by SamReidHughes on Friday December 16, 2016 @04:52PM (#13197219)
Well I don't know about that. If you have a datatype whose constructors can't throw, it should still assert at run time about its state that it's been initialized. Like, you have to actually do that, of course. So there's more surface area for mistakes inside the object, but from the outside of the object, which is what I think this sub-thread is talking about, they get caught at run time.
Re:When noexcept? by lmm on Monday December 19, 2016 @09:24AM (#13211536)
It shouldn't have to be assertions though - you should make it correct by, well, construction. When the fields are RAII the constructor can just initialize them. If your class has different possible states and flags it's usually correct to promote the different states to differnt types (subclasses).
Re:When noexcept? by SamReidHughes on Monday December 19, 2016 @08:16PM (#13216525)
I don't think we're disagreeing about anything. The scope of my comment is in reference to "So how do you protect yourself against inadvertently passing around uninitialized objects?" The answer is, with a run-time error.
Re:When noexcept? by praptak on Thursday December 15, 2016 @10:42AM (#13185304)
Why global? Just check if the resource is properly initialized:

    std::ofstream file("example.txt");
    if (!file.is_open()) return;
    // ofstream will close in destructor.
And RAII doesn't even imply that the acquisition must happen via constructor. Edit: Technically it does, but the above pattern would not change if 'file' was a result of calling a free function or whatever.
Re:When noexcept? by lmm on Thursday December 15, 2016 @10:49AM (#13185349)
> Just check if the resource is properly initialized: std::ofstream file("example.txt"); if (!file.is_open()) return;

This is exactly not RAII. You acquired the resource and it might not be initialized. And it has the same problem: it's very easy to pass file around when it's not actually valid.

Re:When noexcept? by praptak on Thursday December 15, 2016 @11:19AM (#13185645)
Yes, this forces you to explicitly check for errors. I find uninitialized objects' badness comparable to the badness of unhandled exceptions - neither is very hard to debug.

But full exception safety is obviously much more than just properly catching exceptions. I mean avoiding subtle traps like the ones here: https://herbsutter.com/gotw/_102/

Re:When noexcept? by realharo on Thursday December 15, 2016 @11:31AM (#13185766)
>I find uninitialized objects' badness comparable to the badness of unhandled exceptions - neither is very hard to debug.

Unless you have a bunch of code that just passes the object around without looking into it (so it won't fail there), and then when the crash finally happens, you have no idea where the invalid object came from.

This is really just another form of the billion dollar mistake, except you have invalid objects instead of null pointers (or in the worst case, you have both).

With an unhandled exception, you always have the context of where and why it happened (assuming you know the reason behind throwing it).

Re:When noexcept? by Aeolos on Thursday December 15, 2016 @02:08PM (#13187220)
This code suffers from a race condition: the file might be deleted/moved/renamed at any point, even if the file.is_open() check succeeds. Code that does not consider this possibility is technically fragile under a wide set of pretty common conditions.
Re:When noexcept? by prodigal_erik on Thursday December 15, 2016 @03:28PM (#13188022)
Windows doesn't allow deleting an open file. Unix allows the deleted file to be used (even for IPC) and doesn't GC it until it's closed. Breaking an open file's user would be bad design requiring some kind of complicated notification system to be at all usable.
Re:When noexcept? by realharo on Thursday December 15, 2016 @10:53AM (#13185378)
I guess another option would be a private constructor + a static factory method that returns an optional.
Re:When noexcept? by user5994461 on Thursday December 15, 2016 @05:31PM (#13189226)
> but exceptions are the only mechanism available for constructors to report failure

use abort() from stdlib.h

Re:When noexcept? by lmm on Friday December 16, 2016 @04:34AM (#13192124)
That's effectively an alternative implementation of exceptions.