News for hackers. Stuff that matters.
Re:When noexcept? by praptak on Thursday December 15, 2016 @09:53AM (#13184922)
There is also the nuclear option of telling the compiler not to use exceptions at all: -fno-exceptions.

In the 3+ years I'm working in C++ without exceptions, I haven't missed them once.

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.
Re:When noexcept? by vog on Thursday December 15, 2016 @09:57AM (#13184960)
I couldn't agree more!

When I first used Qt, I wondered how such a large framework could get along without exceptions. After writing some non-trivial Qt programs, I still noticed that there is really no need for exceptions, except when interfacing non-Qt libraries, where I simply caught and handled the exceptions in a small wrapper.

Finally, I got it: Qt uses the Null-Object pattern (not to be confused with Null values!) consistently, which is really in almost all cases the more elegant solution:

https://en.wikipedia.org/wiki/Null_Object_pattern

Re:When noexcept? by blub on Thursday December 15, 2016 @11:43AM (#13185877)
Null object is not bad, but std::optional has clearer semantics and is probably safer.

This is probably the only sane alternative to exceptions - optional types and result<T, E> types.

Re:When noexcept? by dmm on Thursday December 15, 2016 @09:58AM (#13184965)
Google doesn't use c++ exceptions either. From the Google c++ style guide:

> We do not use C++ exceptions.

https://google.github.io/styleguide/cppguide.html#Exceptions

Re:When noexcept? by cmrdporcupine on Thursday December 15, 2016 @10:42AM (#13185305)
I think the general concensus now is this is a result of history and now the interia of the existing code base and practices makes it implausible to switch. According to some, if Google were starting over now with C++ compilers and language standard at where it is now, likely exceptions would be permitted.
Re:When noexcept? by favorited on Thursday December 15, 2016 @09:35PM (#13190630)
They're certainly not alone. LLVM has a prohibition on exceptions as well, for performance and binary size.

http://llvm.org/docs/CodingStandards.html#do-not-use-rtti-or...

Re:When noexcept? by user5994461 on Thursday December 15, 2016 @05:28PM (#13189192)
I do.

The exceptions crash the program. It's helpful and critical to debug the program and to stop it in case of bug/failure.

That said. I never use exceptions per-se. I just happen to get a std::out_of_bound_exception once in a while (for which there is nothing to be done, except crash :D).

Not sure what would happen if exceptions were disabled. Maybe the error handling would be disabled and program would keep running[1]. Maybe the STL would just call abort() instead of throwing an exception[2].

[1] That would be a disaster. Should never disable exception if so.

Re:When noexcept? by vortico on Thursday December 15, 2016 @10:23AM (#13185135)
A few months ago I came across a great resource with flags for making C++ behave a bit more like C with flags like -fno-exceptions and -fno-rtti. This is useful when working with embedded devices or if it just makes you more comfortable. Does anyone know any more?
Re:When noexcept? by setra on Thursday December 15, 2016 @09:55AM (#13184944)
So how does this actually work with instances that would throw one? Are the standard libraries not very dependent on them?
Re:When noexcept? by pjc50 on Thursday December 15, 2016 @10:53AM (#13185375)
The standard libraries tend to only use them for fatal conditions such as out of memory. The standard library can compile with no-exceptions (see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_except... ); if you mix no-exception and exception-using code, what happens is implementation-dependent but usually involves crashing.
Re:When noexcept? by blub on Thursday December 15, 2016 @11:11AM (#13185570)
vector::at & co and std::stoi & co will throw. Big one: optional will throw.
Re:When noexcept? by brandmeyer on Thursday December 15, 2016 @12:37PM (#13186385)
std::function can throw, too
Re:When noexcept? by jeffinhat on Thursday December 15, 2016 @10:49AM (#13185352)
A lot of large C++ shops re-implement much of the standard library, often to add niche features that don't make sense as a standard. I believe replacing the allocator in a lot of the standard data structures to a non-throwing one will get you pretty close to non-throwing.